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

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.

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.

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!

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.

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”

Leave a Reply

Your email address will not be published.

eighteen + thirteen =