Techoceanhub logo

Let’s start with Kotlin’s basics. chapter-1

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.

Share:

Categories

Archives

Related Posts

Array Destructuring

Array Destructuring Array destructuring is a powerful syntax introduced in ES6 that allows you to unpack elements from an array

Read More »

Explore our Affiliated partner links

hostgator website plan

Hostgator

Best Website hosting plans & Best for 24/7 support.

hostinger webhosting plan

Hostinger

Reasonable website hosting packages for small business & 24/7 Email support

Elementor pro

Design Worpdress website quickly using Elementor Builder and AI

buy elementor website templates

Buy Elementor Website Template

Buy ready made Elementor Website Template from us & Get Free Elementor pro licence for 1 year .