In the previous tutorial we saw the basic Hello World program written in Kotlin. Lets examine it more deeply.

fun main(args: Array<String>) { println("Hello, World!") } 1 2 3 fun main ( args : Array <String> ) { println ( "Hello, World!" ) }

Notice something weird? The main function is defined all by itself, its not inside a class, this is because Kotlin treats functions as first class citizens. “What does that means?” you say, in Java everything is an object, therefore if you want to do anything simple just like declaring a function, you would have to do that inside a class, in Kotlin its different, functions can be the top level members of any file i.e. you can define as many functions as you want without ever writing a class.(We will see more on kotlin functions in the later tutorials).

The main functions takes an Array of Strings as arguments. Inside the function we print “Hello World” in the console using the println() function, which is the shortened version of System.out.println() from Java.

Declaring Variables

To define a variable you can use either var or val keywords. In Kotlin you don’t have to explicitly define the data type of the variable, you can if you want(in some places you will have to define the data type), but the kotlin compiler will automatically infer the data type from the value that you have assigned. Lets see a simple example.

//Defining an int var count = 10 1 2 //Defining an int var count = 10

Here we have use the var keyword which means that the value inside the variable count can be changed.

If you had to explicitly define the data type then you would write something like this.

//Defining an Int specifying the data type var count: Int = 10 //Defining a string var name: String = "Gurleen Sethi" var nameImplicit = "Gurleen Sethi" 1 2 3 4 5 6 //Defining an Int specifying the data type var count : Int = 10 //Defining a string var name : String = "Gurleen Sethi" var nameImplicit = "Gurleen Sethi"

Unlike in Java, you don’t have primitive data types in Kotlin, everything here is an object. So there are no int or double, there are Int and Double.

Remember! In kotlin you cannot leave a variable uninitialised, as you would do in Java, this is because of the fact that Kotlin doesn’t have NullPointerException, so to achieve that this is something that the language requires to have. If you want to set the value of a variable to null then you can do that with the safe-call operator(?) which we will cover in upcoming tutorials, for now just remember that you cannot initialise any variable to null or even leave it uninitialised!

var firstName: String = "Gurleen" var lastName: String // This is wrong and won't compile var age: Int = 21 1 2 3 var firstName : String = "Gurleen" var lastName : String // This is wrong and won't compile var age : Int = 21

Val and Var

There are two ways to define a variable either by using var or val, previously we have seen the use of var keyword. The val keyword is pretty simple as well, all it means that the variable defined is final and the value in it cannot be changed, like in java you define immutable variable by using the keyword final, similarly in Kotlin you would use the keyword val.

val maxLength = 12 // This variable is final, you cannot reassign the value 1 val maxLength = 12 // This variable is final, you cannot reassign the value

Data Types

Kotlin being a modern language supports all the data types.

Double

Float

Int

Long

Short

Byte

Boolean

String

Characters

Arrays

Find the details of basic data types here.

That is all you need to know for now about declaring variables. As you move on with the tutorials you will get much better understanding of variables.

Next Tutorial “Getting Started with Kotlin – Tutorial 2 – Control Flow” >>

<< Previous Tutorial “Getting Started with Kotlin – Tutorial 0 – Installation and Hello World”