X
    Categories: Kotlin

Kotlin Variable Declaration || Kotlin, Android

 

In this tutorial we will go through the variables usage in Kotlin though you have a knowledge of variables in various other programming languages but Kotlin variable has a slightly different type of initialization compared to java.

 

Kotlin variable :

Variables are the basic requirements for any type of program. Even it might be a one page or a big enterprise software variables have their own usage and importance.

In the previous tutorial we have gone through basic usage of method and print to display a message now we will go through basic usage of Kotlin variable.

 

String variable

Strings are mostly used variables out of all other Kotlin variables  if i am wrong in this assumption let me know in comment section below. so let’s see how we used them before and how we are going to use them in Kotlin

 

java:

We can directly assign a values at the time of initialization

String message = "hello world";

 

Or another way of initializing a String

String message;

 

Assigning a value to it later..

message = "hello world";

 

Kotlin:

Kotlin variable also has the same usage of variables as the code written in Kotlin is converted to java, but declaration is different compared to that of java

  1. Var keyword is specified
  2. Name of the variable is specified
  3. Type of the variable is specified using a  ‘ : ‘

 

var message : String

 

and as in the java value can be later assigned

message = "Hello World"

 

or at the time of initialization as

var message : String = "Hello World"

 

That’s ok and simple but lets know further this String message can be redefined any number of times and it accepts every new message and stores replacing the previous message.

 

Static String Declaration

Now let’s see a String which cannot be redefined again and again.This is called a static way of declaring a String it has its own importance in programming world.

 

java:

static final String message = "Hello World";

try to redefine the Kotlin variable let me know result in comment section

 

Kotlin:

Yes Kotlin also has static way of  declaring a Kotlin variable as val

  1. Val keyword is specified for static variable
  2. Name of the variable is specified
  3. Type of the Kotlin variable is specified using a  ‘ : ‘

 

val message : String = "Hello World"

 

Now try to redefine this and let me know..

 

Integer Variable

Actually after going through the above info on Kotlin variable you can declare an integer variable in same way but there is a small difference you may guess it but lets see

 

java:

int count = 10;

 

Kotlin:

You cannot use small ‘ i ‘ here in declaring a integer variable instead use capital ‘ I ‘ as below

 

var count : Int = 10

 

Static Integer Variable

 

java:

static final int count = 1;

 

Kotlin:

Val is used for static declarations of Kotlin variables.

val count : Int = 10

If you have any query on this tutorial on Kotlin variable do let us know in the comment section below.If you like this tutorial do like and share us for more interesting updates..

 

For more detailed explanation of kotlin variable and video tutorials on android kotlin may visit

 

abhishek:
Related Post

This website uses cookies.