val variables and var variablesIntroduction to KotlinSyntaxKotlin uses static typing

Kotlin uses static typing

The biggest difference between Python and Kotlin is that Python uses dynamic typing, Kotlin uses static typing.

Every piece of data handled by a Python or Kotlin program is an object. Every object has a type. The type of an object determines what we can do with the object. Examples of objects are numbers, strings, files, and digital images.

A variable is a name assigned to an object during the execution of a program. The object currently assigned to a name is called the value of the name or variable.

In dynamically typed programming languages like Python, the same name can be assigned to all kinds of different objects:

# This is Python!
m = 17          # int
m = "seventeen" # str
m = 17.0        # float
This is flexible, and makes it easy to quickly write some code. It also makes it easy to make mistakes, though. If you assign an object of the incorrect type, you only find out during the execution of the program that something doesn't work—you get a runtime error.

Kotlin is a statically typed language. This means that a variable can only be assigned to objects of one fixed type, the type of the variable:

var m : Int = 17
m = 18          // ok
m = "seventeen" // error!
m = 18.0        // error!

The advantage of a statically typed language is that the compiler can catch type errors during the compilation, before the program is even run. A good compiler can also generate more efficient code for a statically type language, as the type of objects is already known during the compilation. Consider the following Python function:

# This is Python!
def test(a, b):
  print(a + b)
The + operator here could mean integer addition, float addition, string concatenation, or list concatenation, depending on the type of the parameters a and b. The code created for this function must look at the types of the arguments and determine which method to call. Now consider the following Kotlin function:
fun test(a : Int, b : Int) {
  println(a + b)
}
Here it is clear to the compiler that the + operator means integer addition, and so the code for the function can immediately add the two numbers.

Other statically typed languages are Java, C, and C++. One disadvantage of statically typed languages is that one has to write type names everywhere, leading to code that is much more verbose than code in, say, Python. Indeed, in languages like Java or C, the programmer has to write down the type for every variable she uses:

/* This is C */
int m = 17;
float f = 17.0;

Kotlin makes our life easier and our code shorter by using type inference. If Kotlin can detect what the type of a variable name should be, then we do not need to indicate the type:

>>> var m : Int = 17      // ok
>>> var n = 18            // also ok
>>> var f = 19.5
>>> var h = "Hello World"

If you are not sure what type the compiler has selected for a variable, you can use the :: notation in the interactive mode:

>>> ::m
var Line_12.m: kotlin.Int
>>> ::n
var Line_13.n: kotlin.Int
>>> ::f
var Line_14.f: kotlin.Double
>>> ::h
var Line_15.h: kotlin.String
val variables and var variablesIntroduction to KotlinSyntaxKotlin uses static typing