Twitter LogoFacebook Logo
Variables in Kotlin
By: King

A variable is a name to represent any value. In a computer, it is a name for a slot in the RAM (Random Access Memory). 


var [name] : [type] = [value]

val [name] : [type] = [value]

The keywords "var" and "val" are used at the beginning of the declaration of the variable to let the computer know that it is indeed a variable.

The differences between "var" and "val" is that "var," means the value can be changed. "val," means that the value is FINAL and cannot be changed once it is set.


var str: String = "My First String!"

Since it is a "var," we can change its value.

str = "Changed Value"

We can ignore the type if we declare and initialize the variable in the same line (inferred).

var str = "My First String!"

Data Types

Here are some primitive types available

String: Text 

int: Whole numbers

float: Decimal numbers

double: Large decimal numbers

boolean: true or false

char: Single letter

Sign In