Kotlin

Difference between val and var in Kotlin

Difference between val and var in Kotlin
155views

Hi, Developer in this Kotlin article we learn about the difference between Val and Var in Kotlin. There are two main keywords that you should study when it comes to Kotlin variables: var, and val.

var can be assigned multiple times and is called the mutable variable. val, on the other hand, is a constant variable that can’t be assigned multiple times. It can only be Initialized once and can’t be changed again. This variable is called the immutable variable.

Difference between val and var in Kotlin

Kotlin Var keyword

To introduce a variable in Kotlin, we use the keyword var. Let’s create a variable called subject and give it the value “maths”.

var subject = Maths

  • var refers to the keyword that defines a variable.
  • subject refers to the name of the variable. The identifier is the name of the variable.
  • Maths refers to the value for the variable.

This would have been the result if we had used Java over code.

String subject = “Maths”.

Java uses the datatype first, then identifier and equals sign. value is added to the variable. Finally, java ends it with a semicolon.

in Kotlin starts with the var keyword. Next, the identifier the variable equals sign, and the values the variable. does not contain a semicolon. We don’t use semicolons in Kotlin. Kotlin is smart enough to recognize when the statement ends

Kotlin cleverly defines the data type by taking into account the value of the variable.

var subject = Maths

As we have added a String value as a first step to the variable subject, Kotlin will recognize that data type as String.

Android Studio will display an error message if I assign an Int value such as this subject = 12, or a double value such as this subject = 123.45. Code will not compile.

Kotlin recognizes the subject’s data type String so we can only assign a String value to it later. This one: Subject = “Science”

Although we didn’t define a data type explicitly, Kotlin was able to recognize it by looking at its value.

This code will compile perfectly.

Let’s define two more Kotlin variables. One int variable named marks and similarly one double variable named average.

We didn’t explicitly define a data type, but Kotlin is intelligent enough to recognize the data type by looking at its value.

Let’s make a toast message

Let me now give you a real example. Let me demonstrate how you can use these variables to create a Toast message. It is nearly the same process to write a Toast message as in Java.

  • To get the application’s context, in java instead of getApplicationContext() we just use the applicationContext property in Kotlin.
  • You can use variables in a String statement only by inserting $ sign before the variable name.

You will see the Toast message “I got 80 marks in Science” if you are the one who runs the project. My average was 76.7

Complete Code:- 

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var subject = "Maths"
        subject = "Science"
        var marks = 85
        var average = 79.8
        Toast.makeText(applicationContext,"I got $marks for $subject . My average was $average ",Toast.LENGTH_LONG).show()
    }
}

If you run the project, you will be glad to see the Toast message saying ” I got 85 marks for Science. My average was 79.8 ”

Kotlin Keyword Val

To create variables, we used the last keyword in Java. A variable declared with this final keyword cannot be modified.

We can also use val keywords in Kotlin.

Let’s change the variable type to val. Subject = Maths

We cannot now assign another value to this variable, such as “Science”. Android studio will display an error message if we do this and the code will not be compiled. Immutable variables are variables in the Kotlin programming language that have been defined using val keyword. Variables that are defined using the var keyword will be known as mutable variables.

Complete Code:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val subject = "Maths"
        var marks = 80
        var average = 76.8
        Toast.makeText(applicationContext,"I got $marks for $subject . My average was $average ",Toast.LENGTH_LONG).show()
    }
}