Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 362 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the difference between var and val in Kotlin?

#11

+----------------+-----------------------------+---------------------------+
| | val | var |
+----------------+-----------------------------+---------------------------+
| Reference type | Immutable(once initialized | Mutable(can able to change|
| | can't be reassigned) | value) |
+----------------+-----------------------------+---------------------------+
| Example | val n = 20 | var n = 20 |
+----------------+-----------------------------+---------------------------+
| In Java | final int n = 20; | int n = 20; |
+----------------+-----------------------------+---------------------------+

[Reference][1]


[1]:
Reply

#12
Both variables are used as initialising

* val like a constant variable, It can be readable, and the properties of a val can be modified.

* var just like a mutable variable. you can change the value at any time.
Reply

#13
**Var** means **Variable**-If you stored any object using **'var'** it could change in time.

For example:

fun main(args: Array<String>) {
var a=12
var b=13
var c=12
a=c+b **//new object 25**
print(a)
}



**Val** means **value**-It's like a **'constant'** in java .if you stored any object using 'val' it could not change in time.

For Example:

fun main(args: Array<String>) {
val a=12
var b=13
var c=12
a=c+b **//You can't assign like that.it's an error.**
print(a)
}


Reply

#14
VAR is used for creating those variable whose value will change over the course of time in your application. It is same as VAR of swift, whereas VAL is used for creating those variable whose value will not change over the course of time in your application.It is same as LET of swift.
Reply

#15
val - Immutable(once initialized can't be reassigned)

var - Mutable(can able to change value)

Example

in Kotlin - val n = 20 & var n = 20

In Java - final int n = 20; & int n = 20;
Reply

#16
variables defined with **var are mutable(Read and Write)**

variables defined with **val are immutable(Read only)**

Kotlin can remove findViewById and reduce code for setOnClickListener in android studio. For full reference: ***[Kotlin awesome features][1]***

value of mutable variables can be changed at anytime, while you can not change value of immutable variables.

***where should I use var and where val ?***

use var where value is changing frequently. For example while getting location of android device

var integerVariable : Int? = null

use val where there is no change in value in whole class. For example you want set textview or button's text programmatically.

val stringVariables : String = "Button's Constant or final Text"


[1]:

[To see links please register here]

Reply

#17
In Kotlin we use `var` to declare a **variable**. It is **mutable**. We **can** change, **reassign** variables. Example,

fun main(args : Array<String>){
var x = 10
println(x)

x = 100 // vars can reassign.
println(x)
}

We use val to declare **constants**. They are **immutable**. **Unable** to change, **reassign** vals. `val` is something similar to `final` variables in java. Example,

fun main(args : Array<String>){
val y = 10
println(y)

y = 100 // vals can't reassign (COMPILE ERROR!).
println(y)
}
Reply

#18
If we declare variable using `val` then it will be **read-only** variable. We cannot change it's value. It's like **final** variable of **Java**. It's `immutable`.

But if we declare variable using `var` then it will be a variable which we can **read** or **write**. We can change it's value. It's `mutable`.

data class Name(val firstName: String, var lastName: String)

fun printName(name: Name): Name {
val myName = Name("Avijit", "Karmakar") // myName variable is read only
// firstName variable is read-only.
//You will get a compile time error. Val cannot be reassigned.
myName.firstName = myName.firstName
// lastName variable can be read and write as it's a var.
myName.lastName = myName.lastName
return myName
}

`val` cannot be initialized lately by the keyword `lateinit` but non-primitive `var` can be initialized lately by the keyword `lateinit`.
Reply

#19
I get the exact answer from [de-compiling Kotlin to Java][1].

If you do this in Kotlin:

data class UsingVarAndNoInit(var name: String)
data class UsingValAndNoInit(val name: String)

You will get **UsingVarAndNoInit**:

package classesiiiandiiiobjects.dataiiiclasses.p04variiiandiiival;

import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;

public final class UsingVarAndNoInit {
@NotNull private String name;

@NotNull
public final String getName() {
return this.name;
}

public final void setName(@NotNull String string) {
Intrinsics.checkParameterIsNotNull((Object) string, (String) "<set-?>");
this.name = string;
}

public UsingVarAndNoInit(@NotNull String name) {
Intrinsics.checkParameterIsNotNull((Object) name, (String) "name");
this.name = name;
}

@NotNull
public final String component1() {
return this.name;
}

@NotNull
public final UsingVarAndNoInit copy(@NotNull String name) {
Intrinsics.checkParameterIsNotNull((Object) name, (String) "name");
return new UsingVarAndNoInit(name);
}

@NotNull
public static /* bridge */ /* synthetic */ UsingVarAndNoInit copy$default(
UsingVarAndNoInit usingVarAndNoInit, String string, int n, Object object) {
if ((n & 1) != 0) {
string = usingVarAndNoInit.name;
}
return usingVarAndNoInit.copy(string);
}

public String toString() {
return "UsingVarAndNoInit(name=" + this.name + ")";
}

public int hashCode() {
String string = this.name;
return string != null ? string.hashCode() : 0;
}

public boolean equals(Object object) {
block3:
{
block2:
{
if (this == object) break block2;
if (!(object instanceof UsingVarAndNoInit)) break block3;
UsingVarAndNoInit usingVarAndNoInit = (UsingVarAndNoInit) object;
if (!Intrinsics.areEqual((Object) this.name, (Object) usingVarAndNoInit.name)) break block3;
}
return true;
}
return false;
}
}

You will also get **UsingValAndNoInit**:

package classesiiiandiiiobjects.dataiiiclasses.p04variiiandiiival;

import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;

public final class UsingValAndNoInit {
@NotNull private final String name;

@NotNull
public final String getName() {
return this.name;
}

public UsingValAndNoInit(@NotNull String name) {
Intrinsics.checkParameterIsNotNull((Object) name, (String) "name");
this.name = name;
}

@NotNull
public final String component1() {
return this.name;
}

@NotNull
public final UsingValAndNoInit copy(@NotNull String name) {
Intrinsics.checkParameterIsNotNull((Object) name, (String) "name");
return new UsingValAndNoInit(name);
}

@NotNull
public static /* bridge */ /* synthetic */ UsingValAndNoInit copy$default(
UsingValAndNoInit usingValAndNoInit, String string, int n, Object object) {
if ((n & 1) != 0) {
string = usingValAndNoInit.name;
}
return usingValAndNoInit.copy(string);
}

public String toString() {
return "UsingValAndNoInit(name=" + this.name + ")";
}

public int hashCode() {
String string = this.name;
return string != null ? string.hashCode() : 0;
}

public boolean equals(Object object) {
block3:
{
block2:
{
if (this == object) break block2;
if (!(object instanceof UsingValAndNoInit)) break block3;
UsingValAndNoInit usingValAndNoInit = (UsingValAndNoInit) object;
if (!Intrinsics.areEqual((Object) this.name, (Object) usingValAndNoInit.name)) break block3;
}
return true;
}
return false;
}
}

There are more examples here:

[To see links please register here]


[1]:

[To see links please register here]

Reply

#20
In short, **val** variable is final (not mutable) or constant value that won't be changed in future
and
**var** variable (mutable) can be changed in future.

class DeliveryOrderEvent(val d : Delivery)
// Only getter

See the above code. It is a model class, will be used for data passing. I have set **val** before the variable because this variable was used to get the data.

class DeliveryOrderEvent(var d : Delivery)

// setter and getter is fine here. No error

Also, if you need to set data later you need to use **var** keyword before a variable, if you only need to get the value once then use **val** keyword
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through