Today is the day we are going to start with the kotlin basics., you know what kotlin language is? This language is all about developing applications despite, any kind of development, whether it is server side, client side or Android. But here I am just going to indicate some basics. Lets study together.
In kotlin we declare variables using var or val, but there is a high difference between them. Before understanding val and var, we have to understand how to declare variables.
Let’s see a basic example:-
var id=5
We can declare variables in kotlin in many ways using var or val like:-
var id:Int=5
val id:Int=5
val id=5
Above is a simple illustration of how to declare variables but we use val or either var for declaration.
In kotlin there is one more important aspect to understand in kotlin that it does not make use of semicolon. I have posted a simple example of how to declare a variable in kotlin.
package adapter.manasvi.com.varexample
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
var i=5
override fun onCreate(savedInstanceState: Bundle?) {
i=6;
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var tv=findViewById(R.id.textView)as TextView
tv.setText("value is"+i)
}
}
In above example the emulator will give you an output as 6, but you can declare your variable in either manner as defined in above example or like
var i:Int=5
that’s the way of kotlin 🙂
Feel free to comment and try above given example.