You are here
Home > Kotlin

Kotlin Basics

kotlin variables declaration

Kotlin Basics

In this post we will discuss basic concept of kotlin like variable declaration, main function, print statement.

Kotlin main function:

  • Same as all other languages kotlin also has main function. Like java has Public static void main (String args[]){}.
  • The main function is the entry point of the program to start execution.

Fun main (args: Array<string>)

{

// kotlin code

}

  • Variable declaration, class object declaration, function calling all are this place inside the main function.

Variable Declaration:

  • In kotlin we declare variable using two keywords:  val ,var

Val:

  • Syntax: Val variable_name: datatype = value;
  • When we declared variable using val we must have to initialize at the time of declaration. Datatype specification is optional. When we not specify datatype, it will consider datatype base on the initialize value.
  • Val is same as declaring variable in java. It is used to declare read-only variable.
  • Val is immutable, means once we assign value we cannot change its value.
  • Ex: val name: string= “Kotlin”;

Var:

  • Syntax: Var variable_name: datatype = value;
  • when we declared variable using var it is not necessary to initialize it.
  • var is mutable, means we can change its value at any time once it is declare.

Var and Val difference:

Var val
Syntax:

Var variable_name: datatype = value;

Syntax:

Val variable_name: datatype = value;

Var is mutable means we can reassign it’s value. Val is not mutable means we cannot reassign it’s value.
Ex: var name: string;

name=”riya”;

name=”kotlin”;   //valid

Ex: val name: string= “Kotlin”;

Name=”Riya”;     //not valid

Data type specification is optional. If not specified it will consider based on value initialize. Data type specification is optional.
It is not necessary to initialize variable at the time of variable declaration. Must be initialize when it is created.

To print something to screen

In kotlin two statements are used to print in to screen.

1) print(“expression”);

  • It prints specified expression into the screen.
  • Ex: print(“Kotlin”);

2) println(“expression”);

  • It prints specified expression into the new line.
  • Ex: println(“Kotlin”);

Comments in Kotlin:

  • Comment is same as all other languages like java.
  • For single line comment use // at the beginning of the line.
  • For multiline comment use /*  */ at the start and end of the block.

Example:

// this is single line comment

/*

This is multi-line comment

*/

Example:

fun main(args:Array<String>)
{
//single line comment
/*
This is
Multi-line comment

*/

//variable declaration using val
val s1:String=”Kotlin”;   //with datatype specification
val a=20;                 //without datatype specification
// a=10;                      //not valid

//variable Declaration
var s2:String=”Riya”;
var b=21;
s2=”value_change”;

//print value
print
(“val s1=”+s1);
println(“val a=”+a);
println(“var s2=”+s2);
println(“var b=”+b);
}

output:


Keep Visiting TechwithR.com for more upcoming top technical articles and also see my new blog Mag4info.

Detail course about DBA visit TechNet Consultancy

You can also by SQL 70-761 book write by me from Amazon.

You can also by SQL 70-761 book write by me from Amazon.
You can also by Kotlin Book write by me from Amazon.

 

Leave a Reply

Top