Kotlin Basics

The Unit return type declaration is optional for functions. The following codes are equivalent.

Single-Expression functions:When a function returns a single expression, the curly braces can be omitted and the
body is specified after = symbol

fun sum(a : Int, b : Int) : Int = a  + b

Explicitly declaring the return type is optional when this can be inferred by the compiler

fun sum(a : Int, b : Int) = a  + b

String interpolation: Using string values is easy.

In java:

int num=10
String s = "i =" + i;

In Kotlin:

val num = 10
val s = "i = $num"

In Kotlin, the type system distinguishes between references that can hold null (nullable references) and those
that can not (non-null references). For example, a regular variable of type String can not hold null:

var a: String = "abc"
a = null // compilation error

To allow nulls, we can declare a variable as nullable string, written String?:

var b: String? = "abc"
b = null // ok

In Kotlin,== actually checks for equality of values. By convention, an expression like a == b is translated to

a?.equals(b) ?: (b === null)

Lasă un răspuns

Adresa ta de email nu va fi publicată. Câmpurile obligatorii sunt marcate cu *

Acest sit folosește Akismet pentru a reduce spamul. Află cum sunt procesate datele comentariilor tale.