Meet the Kotlin Programming Language

Apr 10 2024 · Kotlin 1.9, Android 14, Android Studio Hedgehog

Lesson 02: Working with Types

Demo

Episode complete

Play next episode

Next
Transcript

Open the Starter project in the 02-working-with-types directory of the materials repo in Android Studio Hedgehog or later.

Wait for the project to finish building.

Create a new scratch file in the com.yourcompany.android.meetkotlin package.

In this lesson, you’ll practice working with types and nullability.

In your scratch file, type the following snippet:

val age: Int = 28
println("I am $age years old!")

In the snippet above you:

  1. Created an immutable variable of type Int called age and assigned it a value of 28.
  2. Then used println() to print out a message that uses the age variable.

To hold larger values, you can use the Long type. It’s an integer type with a larger value range than Int.

val peopleOnEarth: Long = 8000000000

Note: To format large numeric values, you can use an underscore. So the value above can be re-written as val peopleOnEarth: Long = 8_000_000_000. This makes the value a lot more readable.

For holding fractional values, you use Float.

val fraction: Float = 0.5F
println("Fraction 1/2 is = $fraction")

In the snippet above you:

  1. Created an immutable variable of type Float called fraction and assigned it a value of 0.5.
  2. Then used println() to print out a message that uses the fraction variable.

If you want to hold values that have a large decimal part, you can use Double.

val pi: Double = 3.1428571429
println("Pi is = $pi")

In your scratch file, enter the following snippet:

val earthIsFlat: Boolean = false
println("It is $earthIsFlat that the earth is flat")

val marsIsRed: Boolean = true
println("It is $marsIsRed that Mars is red")

In the snippet above you:

  1. Created an immutable variable of type Boolean called earthIsFlat and assigned it a value of false.
  2. Then used println() to print out a message that uses the earthIsFlat variable.
  3. Created an immutable variable of type Boolean called marsIsRed and assigned it a value of true.
  4. Then used println() to print out a message that uses the marsIsRed variable.

Now that you know how to use the Boolean type, it’s time to work with characters and strings.

Enter the following snippet in your scratch file:

val character: Char = 'A'
println("First letter of the alphabet is $character")

In the snippet above you:

  1. Created an immutable variable of type Char called character and assigned it a value of A.
  2. Then used println() to print out a message that uses the character variable.

Now, try out strings. To do so, enter the following snippet in your file:

val name: String = "John"
println("My name is $name")

In the snippet above you:

  1. Created an immutable variable of type String called name and assigned it a value of John.
  2. Then used println() to print out a message that uses the name variable.

You can use an array to store multiple values of the same type. Paste the following snippet in your editor:

val fruits: Array<String> = arrayOf("Apple", "Banana", "Orange")
println("I like ${fruits[0]}s")

In the snippet above you:

  1. Created an array of type String called fruits.
  2. Stored Apples, Bananas, and Oranges as values.
  3. Then used the println() to print out a message that uses the fruit stored at index 0 in the fruits array.

Note: Arrays are ‘zero indexed’ which means that the first element in an array is 0, so always remember to subtract one from the index of the element if you’re used to counting starting at one

Kotlin creates a distinction between types that can hold null and types that can’t hold null. This distinction helps avoid errors when dealing with null values and makes the code more robust.

Paste the following code in your scratch file:

var message: String? = "Hello"
println(message)
message = null
println(message)

In the snippet above you:

  1. Created a mutable variable of type String? (which can be read as optional string, meaning it can hold a null value as well as a String) called message and assigned it a value “Hello”.
  2. You then used println() to print out the message variable to the console.
  3. You then reassigned the message variable to null.
  4. You then used the println() to print the value of the message variable to the console, which, in this case, is null.

In Kotlin, when working with basic types, it can often feel verbose and repetitive to declare types everywhere. Kotlin has a nifty feature called type inference that makes working with types a breeze. The compiler can infer the variable type from the initializer expression with type inference.

This means that for the expression val name = "John" the compiler can infer that the variable name type is String, so you don’t have to specify it explicitly.

Try it out yourself by pasting the following snippet in your scratch file:

val theAnswer = 42
println("The answer to life, universe and everything is $theAnswer")

In the snippet above you:

  1. Created an immutable variable called theAnswer and assigned it a value of 42.
  2. You then used println() to print out a message that uses theAnswer variable.
  3. The compiler can infer the type of theAnswer, i.e., Int, so you don’t need to specify it manually.

That ends this demo. Continue with the lesson for a summary.

See forum comments
Cinema mode Download course materials from Github
Previous: Instruction Next: Conclusion