(+91) 113 551 1687 contact@syncsas.com New Delhi

Kotlin Data Types & Basic Syntax

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.

In Kotlin, everything is an object in the sense that we can call member functions and properties on any variable. Some of the types can have a special internal representation – for example, numbers, characters and booleans can be represented as primitive values at runtime – but to the user they look like ordinary classes. In this section we describe the basic types used in Kotlin: numbers, characters, booleans, arrays, and strings.

Kotlin represents character using char. Character should be declared in a single quote like ‘c’. Please enter the following code in our coding ground and see how Kotlin interprets the character variable. Character variable cannot be declared like number variables. Kotlin variable can be declared in two ways – one using “var” and another using “val”.

The difference in using var and val is discussed later in the article. For now, let’s focus on variable declaration.

Here, language is a variable of type String, and score is a variable of type Int. You don’t have to specify the type of variables; Kotlin implicitly does that for you. The compiler knows this by initializer expression (“French” is a String, and 95 is an integer value in the above program). This is called type inference in programming.