This article is part of the Kotlin Learning Series for programmers. If you read the articles in order, you'll benefit greatly from this Series.
Previous Articles,
Introduction
In this article, you will discover the basics of Kotlin programing language, data types, operators, variables, control structures, and nullable versus non-nullable variables. You can dive into each of the topics independently.
What you'll learn
- Kotlin's data types, operators, and variables
- How Booleans and conditions work in Kotlin
- Working with nullable and non-nullable variables
- arrays, lists, and loops in Kotlin.
What you'll do
1. Variable, Operators, and types in Kotlin
All languages differ, but they also have many similar elements and features. The role of a variable is straightforward: assign a value and give it a name to be used later. To improve the efficiency and readability of the code, Kotlin takes this specific idea and adds a few additional rules.
There are two types of variables,
- val (unchangeable)- values of this variable can be assigned only once.
- var (changeable)- values of this can be reassigned or changed to them.
Kotlin variables have a simple syntax like the one below. The first word is used to declare which type of variable it is, the second is the variable's name, and after :(colon) is the type of var, and the optional assignment takes place after the equals (=) mark.
// Using var
var welcome:String = "Hello learners"
// Using val
val welcome_again:String = "Hello again learners"
Note. As Kotlin does not, by default, support null values, you cannot declare a variable using only a declaration and without immediately assigning it a value. Due to Kotlin's concept of emphasizing readability, you must explicitly assign the value (even null) when declaring the variable's initial state. But if you want to use a variable whose value is assigned by the runtime, you can use a keyword lateinit before the variable declaration.
lateinit welcome_after:Int
Operators in Kotlin
To learn about Operators and types in Kotlin, you can use Kotlin REPL very wisely. As discussed in the previous article, you can use Kotlin REPL on IntelliJ IDEA by selecting Tools->Kotlin->Kotlin REPL. This will give you the internal store type that Kotlin runs about the data.
Use Kotlin operator + (plus), - (minus), * (multiply), / (division). Kotlin supports Int, Long, Double, and Float types of numbers.
To evaluate the expression, press Ctrl + Enter.
You can use some of these to compare the results using integers and doubles. Also, you can call some of these techniques at the numbers listed below.
2+4
res0: kotlin.Int = 6
5/2
res1: kotlin.Int = 2
5/2.0
res2: kotlin.Double = 2.5
2.times(3)
res3: kotlin.Int = 6
3.5.plus(4)
res4: kotlin.Double = 7.5
2.4.div(2)
res5: kotlin.Double = 1.2
You cannot assign a short value to a long variable or a byte to an int because Kotlin does not implicitly convert between number types. This is because the conversion of this type can occur errors in programs, although you can assign values of different types by casting. See below examples
val i: Int = 25
val i1: Double = (Int) i
// In java you can do like that but this will not work in kotlin instead use this
val i1: Double = i.toDouble()
Kotlin lets you insert underscores where it makes sense to you to make long numeric constants easier to read. Try typing various numerical constants.
val mobile_no = 789_123_6166
val card_details = 9999_9999_9999 L
//this (underscore) will not effect you numbers, it exactly be like
mobile no - 7891236166
card details - 9999999999
Type in Kotlin
Kotlin is Strongly typed, Kotlin compiler can infer the type of variables, so you don't need to pre-declare the type of it (duck-typing). We can define the variable and specify the type explicitly.
Following the concept, everything in Kotlin is an object; it makes sense that we can call its member function and properties on any of the variables. Numbers, characters, and booleans may represent primitive values at the runtime, but they look like an ordinary class.
The basic types are,
- Number and their unsigned counterparts
- Booleans
- Characters
- String
- Arrays
Numbers in Kotlin
There are several built-in types for representing numbers in Kotlin. There are four types of integers, each having a distinct size and thus a different value range:
1. Integer types
Type |
Size (bits) |
Min value |
Max value |
Byte |
8 |
-128 |
127 |
Short |
16 |
-32768 |
32767 |
Int |
32 |
-2,147,483,648 (-231) |
2,147,483,647 (231 - 1) |
Long |
64 |
-9,223,372,036,854,775,808 (-263) |
9,223,372,036,854,775,807 (263 - 1) |
2. Floating-point types
Type |
Size (bits) |
Significant bits |
Exponent bits |
Decimal digits |
Float |
32 |
24 |
8 |
6-7 |
Double |
64 |
53 |
11 |
15-16 |
3. Literal constants for numbers,
val oneMillion = 1_000_000
val creditCardNumber = 1234_5678_9012_3456L
val socialSecurityNumber = 999_99_9999L
val hexBytes = 0xFF_EC_DE_5E
val bytes = 0b11010010_01101001_10010100_10010010
4. Unsigned integer types
Unsigned numbers are mostly used to express positive values by using the entire bit range of an integer.
For unsigned integer numbers, Kotlin offers the following kinds in addition to integer types:
UByte
: an unsigned 8-bit integer ranging from 0 to 255
UShort
: an unsigned 16-bit integer ranging from 0 to 65535
UInt
: an unsigned 32-bit integer ranging from 0 to 2^32 - 1
ULong
: an unsigned 64-bit integer ranging from 0 to 2^64 - 1
Similarly, you can use other unsigned numbers, arrays, and integers literals.
Booleans in Kotlin
Boolean objects are represented by the type Boolean and can have true or false values. Boolean? It also exists, and It also has a value of null.
Built-in boolean operations include:
||
– disjunction (logical OR)
&&
– conjunction (logical AND)
!
– negation (logical NOT)
See the below example,
println(true || false) // || print when any value is true
println(true && false) // && print when both value is true
println(!true) // ! print when opposite value is true
Characters in Kotlin
To represent a single character, a Char variable is used. Char is represented by a single literal quote, e.g., 'a'. Starting from an escaping backslash, special characters are used. There is support for the following escape sequences,
\t
– tab
\b
– backspace
\n
– new line (LF)
\r
– carriage return (CR)
\'
– single quotation mark
\"
– double quotation mark
\\
– backslash
\$
– dollar sign
You can use the '/unicode_value' in code to encode any Unicode or special character.
val myChar: Char = 'a'
println(myChar)
println('\n') // Prints an extra newline character
println('\uFF00')
Strings in Kotlin
Kotlin String is similar to the Java programming language string; you can define a string between two double quotes (") to use and a single quote ( ' ) for single char, a string template can be made by the '$' variable, which the text will replace.
val str = "hello 123"
The string is like an array of characters, so you can access any char of the string using stringe_name[index] and iterate through it.
Escaped strings - contain escape characters.
val s = "Hello, world!\n"
Raw strings
Raw strings can include blank lines and any text. It has no escaping, is separated by a triple quote (""), and is free to contain newlines and any other characters:
val text = """
|Tell me and I forget.
|Teach me and I remember.
|Involve me and I learn.
|(Benjamin Franklin)
""".trimMargin()
String template
When you wish to give a string a real-time value, a string template comes into play. An expression that begins with a dollar symbol ($) calculates a changeable value, and concatenation calculates a string's value.
val limit: Int = 5
println("you can't use more than ${limit} value ")
// you can't use more than 5 value
Arrays in Kotlin
The Array class in Kotlin features get() and set() methods and size properties. It also supports an iterator to interpolate values.
Use the arrayOf() function to use an array.
Kotlin can also generate an array of a specific type, such as an array of int, byte, or short, without incurring the complexity of boxing. They share the same properties and methods as the Array class.
val my_array = arrayOf(1, "two", 3)
val x: IntArray = intArrayOf(1, 2, 3)
my_array.forEach {
println(it)
}
x.forEach {
println(it)
}
2. Conditions and Booleans in Kotlin
Kotlin condition checking has an operator like another language. These are equal to, greater than, and so on (<
, ==
, >
, !=
, <=
, >=
).
If expression
In Kotlin, the if condition returns a value. Kotlin also doesn't have a ternary operator because if the condition can full fill all cases. See ex.
var max = a
if (a < b) max = b
// With else
if (a > b) {
max = a
} else {
max = b
}
// As expression
max =
if (a > b) a
else b
// You can also use `else if` in expressions:
val maxLimit = 1
val maxOrLimit =
if (maxLimit > a) maxLimit
else if (a > b) a
else b
Although if expression can be used to give values to variables, else must always be used in that situation.
val max =
if (a > b) {
print("Choose a")
a
} else {
print("Choose b")
b
}
When expression in Kotlin
Kotlin has a nicer way of writing if / else if / else block, which is "when", When defining a condition statement with multiple options, it is similar to a switch statement in another language.
For example,
when (x) {
1 -> print(" 1 is correct ")
2,4 -> print (" 2 or 4 is there ")
in 5..10 -> print(" is between 5 to 10 ")
is String -> print("is a string")
x.isOdd() -> print("x is odd")
y.isEven() -> print("y is even")
s.toInt() -> print("s encodes x")
else -> false
}
3. Nullability in Kotlin
Null was the most common issue for any developer in earlier languages like Java. Because of nulls, programmers have committed many errors, have problems in their code, and frequently experience crashes. The non-nullable variables have overcome these bugs.
What is Nullability?
In Kotlin, by default, any variable cannot be null except we explicitly say our compiler to make it nullable. That can be done using the question mark operator ?; see below
var a: String = "abc" // Regular initialization means non-null by default
a = null // compilation error
You can declare a variable as a string that can contain nulls by writing String?:
var b: String? = "abc" // can be set to null
b = null // ok
print(b)
You can allow complex data also to have null.
What are '?', '?.', '?:', '!!' operators?
The question mark operator is used to declare the variable as nullable. Now, if we have declared something nullable simply, we have to perform checks before performing any operation on nullable. Let's see how we can perform checks on nullable.
1. Explicility check
The simple check for nullability using the if-else condition.
val l = if (b != null) b.length else -1
2. Safe calls or '?.' operator
The safe call operator ?. is your second choice for getting at a property on a nullable variable.
val a = "Kotlin"
val b: String? = null
println(b?.length)
println(a?.length) // Unnecessary safe call
Safe calls skip the operation if the object is null.
3. '?:' or Elvis operator
The '?:'
the operator is sometimes called the "Elvis operator" because it's like a smiley on its side with a pompadour hairstyle, the way Elvis Presley styled his hair. Read more about the Elvis operator in the Kotlin documentation.
Kotlin is all about making things shorter and easier to read, so as the Elvis operator does, use this operator in place of checking nullability with if-else like below
val l: Int = if (b != null) b.length else -1
with Elvis operator
val l = b?.length ?: -1
4. '!!' or Double bang operator
The not-null assertion operator (!!) converts any value to a non-null type and throws an exception if the value is null. The Double bang operator or '!!' is used if you sometimes want to throw a null pointer exception. See the example below.
val l = b!!.length
This option is for NPE aficionados.
Note - The double-bang operator is typically a bad idea, but it can be used with the legacy Java code.
4. Examine Array, list, and loops in Kotlin
List in Kotlin
A list is a grouping of objects of a similar type. Lists in Kotlin can either be read-only (List) or changeable (MutableList) (List). A list in kotlin is declared by using the listOf function. In kotlin, you don't have to remake a list to modify or add an item to the list. Rather than you can simply make a mutable list using the mutableListOf() function. Use the listOf() and mutableListOf() functions from the standard library to create lists, depending on whether they are read-only or mutable.
//list
val fruits = listOf("orange", "peer", "raspberry") //1
//mutable list
val myList = mutableListOf(1, 2, 3) //2
fun addItem(item: Int) { // 3
myList.add(item)
}
fun getItems(): List < Int > { // 4
return myList
}
fun main() {
println(fruits)
addItem(4) // 5
println("List size: ${getItems().size}") // 6
getItems().forEach { // 7
i - > println("item $i")
}
// getSysSudoers().add(5) <- Error! // 8
}
- Creates a read-only view of the list.
- Creates a
MutableList
.
- Adds a new item to the
MutableList
.
- A function that returns an immutable
List
.
- Updates the
MutableList
. All related read-only views are also updated since they point to the same object.
- Retrieves the size of the read-only list.
- Iterates the list and prints its elements.
- Attempting to write to the read-only view causes a compilation error.
Array in Kotlin
Arrays are basic data types of any language; Kotlin also has arrays. There is no way to modify the Array in kotlin; you must create a new copy after performing operations. The guidelines for using val and var with arrays are the same as those for lists.
Use the arrayOf function to declare a string array. To print it out, use the java.util.Arrays.toString() array function.
val programming_lang = arrayOf("kotlin", "java", "python")
println(Arrays.toString(programming_lang))
You can mix types in an array declared with arrayOf since the elements don't have a type assigned. Create an array with several types.
val mix = arrayOf("mango", 2)
println(Arrays.toString(mix))
Moreover, arrays having a single type for every component can be declared. Use intArrayOf to declare an array of integers (). For arrays of other kinds, there are comparable builders or instantiation functions.
val numbers = intArrayOf(1,2,3)
println(Arrays.toString(numbers))
The ability to start arrays using code rather than setting them to 0 is a good feature of Kotlin. Consider the following illustration:
val array = Array (5) { (it * 2) }
println(Arrays.toString(array))
Loops in Kotlin
After creating a list and Array, we can iterate it through loops; looping in kotlin are 3 types for, while, and do... while, although kotlin also has repeat loops.
Like in most languages, For in Kotlin operates in the same way. Now let's see how for works -
val SuperHeros = listOf("Batman", "Thor", "Superman", "IronMan", "Hulk")
for ( hero in SuperHeros) { // 1
println("i am $hero! ready for the mission")
}
output
i am Batman! ready for the mission
i am Thor! ready for the mission
i am Superman! ready for the mission
i am IronMan! ready for the mission
i am Hulk! ready for the mission
You can also loop through both indexes and elements in Kotlin like below
for ((index, hero) in superHeros.withIndex()) { // 1
println("i am $hero! ready for the mission")
Other properties we can use while looping,
for (i in 1..5) print(i)
β 12345
for (i in 5 downTo 1) print(i)
β 54321
for (i in 3..6 step 2) print(i)
β 35
for (i in 'd'..'g') print (i)
β defg
Now let's see how while, do-while, and repeat works in Kotlin
fun main() {
var apples = 0
while (apples < 10) {
apples++
}
println("$apples apples in the tree\n")
do {
apples--
} while (apples > 10)
println("$apples apples in the tree\n")
repeat(2) {
println("i plus an apple from tree")
}
}
Output
10 apples in the tree
9 apples in the tree
i plug an apple from tree
i plug an apple from tree
Questions
Take a quiz on this article here,
- Question #1. which variable is changeable, val or var?
- Question #2. In Kotlin, can we assign null to any custom class object?
- Question #3. what will be the output of the following code? for (i in 1..5 steps 2)
- Question #4. what will be the output of the following code? for (i in s..z step 2)
- Question #5. for (i in 'd' .. 'm' step 2) print (i)
Summary
This article taught us about types, operators, variables, booleans, conditions, nullability, arrays, and loops. Basic Kotlin learning concepts are similar to learning other languages. The distinctive features include the inability of Kotlin to cast or implicitly covert types. Kotlin has an advantage in finding the type at runtime for variables like val and var and not nullability.
Next in this Series: visit my profile Ravi_sahu.