Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variables in Kotlin, differences with Java: 'var' vs. 'val'?

I am trying to learn Kotlin. What is val, var and internal in Kotlin compared to Java?

In Java:

 RadioGroup radioGroup;
 RadioButton button;
 Button submit;

After converting it shows:

 internal var radioGroup: RadioGroup
 internal var button: RadioButton
 internal var submit: Button
like image 675
Navjot.jassal Avatar asked May 25 '17 10:05

Navjot.jassal


People also ask

What is the difference between const val and val in Kotlin?

In Kotlin, val is also used for declaring a variable. Both "val" and "const val" are used for declaring read-only properties of a class. The variables declared as const are initialized at the runtime. val deals with the immutable property of a class, that is, only read-only variables can be declared using val.

In which way variable declaration in Kotlin differs from Java?

1. The variables can be mutable and immutable. This can also be done in Java (marking variables as final if we don't want it to be modified), but in Kotlin it is much less verbose and much more used: In Kotlin immutable values ​​are preferred whenever possible.

How does Kotlin change val value?

The object stored using val cannot be changed, it cannot be reassigned, it is just like the final keyword in java. val is immutable. Once assigned the val becomes read-only, however, the properties of a val object could be changed, but the object itself is read-only.


2 Answers

val and var are the two keywords you can use to declare variables (and properties). The difference is that using val gives you a read-only variable, which is the same as using the final keyword in Java.

var x = 10    // int x = 10;
val y = 25    // final int y = 25;

Using val whenever you can is the convention in Kotlin, and you should only make something a var if you know that you'll be changing its value somewhere.

See the official documentation about defining local variables and declaring properties.


internal is a visibility modifier that doesn't exist in Java. It marks a member of a class that will only be visible within the module it's in. This is a similar visibility to what the default package visibility gives you in Java (which is why the converter would use it when converting members with package visibility). However, it's not exactly the same. Also, note that it's not the default visibility in Kotlin, classes and their members in Kotlin are public by default.

There is more in the documentation about visiblity modifiers.

like image 169
zsmb13 Avatar answered Oct 22 '22 14:10

zsmb13


val : immutable data variable

var : mutable data variable

When you converted your Java code to Kotlin:

  1. A converter found that you have not initialised variables, so it made them var(mutable) as you will initialise them later.

  2. Probably your variables are unused, so the converter made them internal, guessing you will not use them outside of your package.

For more information on var and var read this, and for internal read this.

like image 30
chandil03 Avatar answered Oct 22 '22 15:10

chandil03