The Programmers Guide To Kotlin - The Basics |
Written by Mike James | ||||||||||||||||||||||||||||||||||||||||
Monday, 01 August 2022 | ||||||||||||||||||||||||||||||||||||||||
Page 3 of 4
Var & ValAlthough it seemed sensible to dive into functions right at the start, because otherwise you can't even make sense of “Hello World”, there are some more basic things to look at. It is important to notice that there are two ways to declare a variable – var and val. If you write: var myVariable:Int = 1 then you will get a true variable that you can read and write. If you write: val myVariable:Int = 1 then you get a read-only variable which you cannot use on the left of an assignment. In most cases you should use var for simple types and val for objects. A more general principle, however, is to always use val unless you are forced to use var by the nature of the algorithm. Notice that although you will hear var described as mutable and val as immutable, this isn't accurate. All val promises is that the variable itself is read-only, and this is a very weak form of immutable. For example, if you declare an instance as: var myObject = MyClass1() then myObject stores a reference to an instance of MyClass1. As it is declared using var, this reference can change. That is, you can write: myObject = MyClass2() and now myObject references an instance of MyClass2. If you had defined myObject as: val myObject = MyClass1() then myObject would be read-only and: myObject = MyClass2() would be indicated as a compile time error because you are trying to change the contents of myObject, which is read-only. As most of the time you don’t change object references, it is a good idea to use val on all variables that are used in object declarations. Notice, however, that the object instance that myObject references can have its properties changed even if referenced by a val. So: val myObject = MyClass1() myObject.myproperty = 10 works, even though myObject is read-only. That is, val makes the contents of myObject immutable but doesn’t make what myObject references immutable. Simple Types & OperatorsIn terms of simple types Kotlin has everything you would expect:
These types are compatible with the types of the same name in Java. You can use decimal, hex and binary constants but octal is banned because o or O looks like a zero and can lead to errors. Some examples are: 0xFF – hex 255 0b10 – binary 2 You also need to know that a Long integer is signified by a trailing L and a Float is signified by a trailing F. A nice touch, though not essential, is that you can break up numeric literals with underscores for readability: 0xFF_FF_FF_FF You can also use Char for characters encoded using Unicode, just like Java, but you cannot use Char in arithmetic expressions. That is, unlike Java, Char is not a numeric type. You can also use Booleans with true and false values. The standard Boolean operations are the same as Java: || lazy OR && lazy AND ! Not Although this is a little advanced, it is worth mentioning now that while Kotlin has bitwise operations it doesn't associate them with operator symbols as Java does. The complete list of bitwise operations, available only for Int and Long,is:
The symbols listed to the right are the Java operators that do the same job – you can't use them in Kotlin. Of course, you can use all of the these functions as infix operators: a or b 1 shr 2 |
||||||||||||||||||||||||||||||||||||||||
Last Updated ( Tuesday, 02 August 2022 ) |