Variables

The var keyword define variable.

The val keyword define constant.

Constant and variable names can contain some unicode characters.

Type annotation can be skipped, when variable initialized by value.

// var <variable-name> [: <type-name>] = <value>

val maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 0

var x = 0.0, y = 0.0, z = 0.0

var welcomeMessage: String
welcomeMessage = "Hello"

val π = 3.14159
val 你好 = "你好世界"
// val 🐶🐮 = "dogcow"   

The ? after type name means that variable may have special null value to indicate a valueless state.

var serverResponseCode: Int? = 404
// serverResponseCode contains an actual Int value of 404

serverResponseCode = null
// serverResponseCode now contains no value

// there is no default value
// even null value you must set
var surveyAnswer: String? = null

if (convertedNumber != null) {
    print("convertedNumber contains some integer value: $convertedNumber")
}