Declaring functions variables classes enums and properties in Kotlin

Basic elements: functions and variables

“Hello World!” in Kotlin

fun main(args: Array<String>) {
println("Hello, world!")
}
  • The fun keyword is used to declare a function.
  • The parameter type is written after its name.
  • The function can be declared at the top level of a file; you don’t need to put it in a class.
  • Arrays are just classes. Unlike Java, Kotlin doesn’t have a special syntax for declaring array types
  • You write println instead of System.out.println. The Kotlin standard library provides many wrappers around standard Java library functions, with more concise syntax, and println is one of them.
  • You can omit the semicolon from the end of a line,

Kotlin function declaration

Kotlin function declaration
Kotlin function declaration

Statements and expressions

In Kotlin, if is an expression, not a statement. The difference between a statement and an expression is that an expression has a value, which can be used as part of another expression, whereas a statement is always a top-level element in its enclosing block and doesn’t have its own value. In Java, all control structures are statements. In Kotlin, most control structures, except for the loops (for, do, and do/ while) are expressions. The ability to combine control structures with other expressions lets you express many common patterns concisely

EXPRESSION BODIES

without curly braces

fun max(a: Int, b: Int): Int = if (a > b) a else b

without return

fun max(a: Int, b: Int) = if (a > b) a else b

Variables in Kotlin

In Java, you start a variable declaration with a type. This wouldn’t work for Kotlin, because it lets you omit the types from many variable declarations. Thus in Kotlin you start with a keyword, and you may (or may not) put the type after the variable name.

Let’s declare two variables:

val question = “The Ultimate Question of Life, the Universe, and Everything”

val answer = 42

This example omits the type declarations, but you can also specify the type explicitly if you want to:

val answer: Int = 42

Just as with expression-body functions, if you don’t specify the type, the compiler analyzes the initializer expression and uses its type as the variable type. In this case, the initializer, 42, has Int type, so the variable will have the same type.

If you use a floating-point constant, the variable will have the type Double:

val yearsToCompute = 7.5e6

MUTABLE AND IMMUTABLE VARIABLES

  • val (from value)—Immutable reference. A variable declared with val can’t be reassigned after it’s initialized. It corresponds to a final variable in Java
  • var (from variable)—Mutable reference. The value of such a variable can be changed. This declaration corresponds to a regular (non-final) Java variable

Even though the var keyword allows a variable to change its value, its type is fixed. For example, this code doesn’t compile:

var answer = 42
answer = “no answer”

Easier string formatting: string templates

Using string templates

fun main(args: Array<String>) {
val name = if (args.size > 0) args[0] else "Kotlin"
println("Hello, $name!")
}

This example introduces a feature called string templates. In the code, you declare a variable name and then use it in the following string literal. Like many scripting languages, Kotlin allows you to refer to local variables in string literals by putting the $ character in front of the variable name. This is equivalent to Java’s string concatenation (“Hello, ” + name + “!”) but is more compact and just as efficient.1 And of course, the expressions are statically checked, and the code won’t compile if you try to refer to a variable that doesn’t exist

If you need to include the $ character in a string, you escape it: println(“\$x”) prints $x and doesn’t interpret x as a variable reference.

You’re not restricted to simple variable names; you can use more complex expressions as well. All it takes is putting curly braces around the expression:

fun main(args: Array<String>) {
if (args.size > 0) {
println("Hello, ${args[0]}!")
}
}

You can also nest double quotes within double quotes, as long as they’re within an expression:

fun main(args: Array<String>) {
println("Hello, ${if (args.size > 0) args[0] else "someone"}!")
}

Classes and properties

Simple Java class Person

/* Java */
public class Person {
private final String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

Person class converted to Kotlin

class Person(val name: String)

Properties

In Kotlin, properties are a first-class language feature, which entirely replaces fields and accessor methods. You declare a property in a class the same way you declare a variable: with val and var keywords. A property declared as val is read-only, whereas a var property is mutable and can be changed

Declaring a mutable property in a class

class Person(
val name: String,
var isMarried: Boolean
)

Using the Person class from Java

/* Java */
>>> Person person = new Person("Bob", true);
>>> System.out.println(person.getName());
Bob
>>> System.out.println(person.isMarried());
true

Declaring enum classes

enum class Color {
RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET
}

Declaring an enum class with properties

enum class Color(
val r: Int, val g: Int, val b: Int
) {
RED(255, 0, 0), ORANGE(255, 165, 0),
YELLOW(255, 255, 0), GREEN(0, 255, 0), BLUE(0, 0, 255),
INDIGO(75, 0, 130), VIOLET(238, 130, 238);
fun rgb() = (r * 256 + g) * 256 + b
}
>>> println(Color.BLUE.rgb())
255

Using when for choosing the right enum value

fun getMnemonic(color: Color) =
when (color) {
Color.RED -> "Richard"
Color.ORANGE -> "Of"
Color.YELLOW -> "York"
Color.GREEN -> "Gave"
Color.BLUE -> "Battle"
Color.INDIGO -> "In"
Color.VIOLET -> "Vain"
}
>>> println(getMnemonic(Color.BLUE))
Battle

Combining options in one when branch

fun getWarmth(color: Color) = when(color) {
Color.RED, Color.ORANGE, Color.YELLOW -> "warm"
Color.GREEN -> "neutral"
Color.BLUE, Color.INDIGO, Color.VIOLET -> "cold"
}
>>> println(getWarmth(Color.ORANGE))
warm

Importing enum constants to access without qualifier

import ch02.colors.Color
import ch02.colors.Color.*
fun getWarmth(color: Color) = when(color) {
RED, ORANGE, YELLOW -> "warm"
GREEN -> "neutral"
BLUE, INDIGO, VIOLET -> "cold"
} 

Leave a Comment