English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn about variables, how to create variables, and the basic data types supported by Kotlin for creating variables.
As you know, variables are locations in memory (storage area) used to store data.
To indicate the storage area, each variable should be assigned a unique name (identifier). Learn more aboutHow to name variables in Kotlin?More Information
To declare a variable in Kotlin, use the 'var' or 'val' keyword. Here is an example:
var language = "French" val score = 95
This article will discuss the difference between 'var' and 'val' later. Now, let's focus on variable declaration.
Here, 'language' is a String type variable, and 'score' is an Int type variable. You do not need to specify the type of the variable; Kotlin will implicitly specify it for you. The compiler determines the type through the initializer expression (in the above program, 'French' is a string, while95Knowing this (that it is an integer value) is called Type Inference.
However, if you need to perform the following operations, you can explicitly specify the type:
var language: String = "French" val score: Int = 95
In the above examples, we initialized the variable during the declaration process. However, this is not necessary. You can declare a variable and specify its type in one statement, and then initialize it in another statement later in the program.
var language: String //Declaration of variables of type String ... .. ... language = "French" //Variable Initialization val score: Int //Declaration of variables of type Int ... .. ... score = 95 //Variable Initialization
Here are some incorrect examples.
var language // Error language = "French"
Here, the type of the variable 'language' is not explicitly specified, nor is the variable initialized during the declaration.
var language: String language = 14 // Error
Here, we try to assign14Assigning an integer value to a variable of a different type (String) will also throw an error.
val(Immutable Reference)- After assignment, variables declared with the 'val' keyword cannot be changed. It is similar to the 'final' variable in Java.
var(Variable Reference)- Variables declared with the 'var' keyword can be changed later in the program. It corresponds to the regular Java variable.
Here are some examples:
var language = "French" language = "German"
Here, the variable language is reassigned the value 'German'. Since the variable is declared with 'var', this code runs perfectly.
val language = "French" language = "German" // Error
In the above example, you cannot reassign the value German to the language variable because the variable is declared using val.
Now that you know what a Kotlin variable is, it's time to learn about the different values that a Kotlin variable can take.
Kotlin is a statically typed language similar to Java. That is, the type of variables is known during compilation. For example,
val language: Int val marks = 12.3
Here, the compiler knows that the type of language is Int, and marks as type Double before compilation.
Built-in types in Kotlin can be classified as:
Numbers (Numbers)
Characters (Characters)
Booleans (Booleans)
Arrays (Arrays)
Kotlin numbers are similar to Java. There are6kinds of built-in types represent numbers.
Byte
Short
Int
Long
Float
Double
The range of Byte data type values is from -128 to 127(8bit signed binary complement integer).
If you can determine that the variable's value is within [-128,127range within, you can use it instead of Int or other integer data types to save memory.
fun main(args: Array<String>) { val range: Byte = 112 println("$range") // The following code shows an error. Why? // val range1: Byte = 200 }
When running the program, the output is:
112
The range of Short data type values is from -32768 to 32767 (16bit signed binary complement integer).
If you can determine that the variable's value is within [-32768,32767inside, you can use it instead of other integer data types to save memory.
fun main(args: Array<String>) { val temperature: Short = -11245 println("$temperature") }
When running the program, the output is:
-11245
The range of Int data type values is from-231 to 231-1(32bit signed binary complement integer).
fun main(args: Array<String>) { val score: Int = 100000 println("$score") }
When running the program, the output is:
100000
If you assign -231 to 231-1between integers and do not explicitly specify its type, then the variable will be of Int type. For example,
fun main(args: Array<String>) { // score is of Int type val score = 10 println("$score") }
If you are using IntelliJ IDEA, you can place the cursor inside the variable and then press Ctrl + Shift + P to view its type.
The range of Long data type values is from -263 to 263-1(64Binary complement integer of a bit symbol).
fun main(args: Array<String>) { val highestScore: Long = 9999 println("$highestScore") }
When running the program, the output is:
9999
If a variable is assigned a value greater than231-1 or less than -231 The integer value (not explicitly specified its type), then the variable will be of type Long. For example,
val distance = 10000000000 // The variable distance of type Long
Similarly, you can use the uppercase letter L to specify that the variable type is Long. For example,
val distance = 100L // The value of distance is of type Long
The Double type is double-precision64fixed-point floating-point.
fun main(args: Array<String>) { // distance is of type Double val distance = 999.5 println("$distance") }
When running the program, the output is:
999.5
The Float data type is single-precision32fixed-point floating-point.
fun main(args: Array<String>) { // distance is of type Float val distance = 19.5F println("$distance") }
When running the program, the output is:
19.5
Please note that we use 19.5F instead of 19.5. This is because 19.5 is a Double literal, so you cannot assign a Double value to the Float typeVariable.
To let the compiler 19.5 If it is treated as Float, you need to use F at the end.
If you are unsure about what numeric value will be assigned to the variable in the program, you can specify it as the Number type. This allows you to assign both integer and floating-point values to the variable (one at a time). For example:
fun main(args: Array<String>) { var test: Number = 12.2 println("$test") test = 12 // Number Smart Cast to Int println("$test") test = 120L // Number Smart Cast to Long println("$test") }
When running the program, the output is:
12.2 12 120
To represent characters in Kotlin, the Char type is used.
Unlike Java, the Char type cannot be considered as a number. Visit this page to learn more aboutJava char typeMore information.
fun main(args: Array<String>) { val letter: Char letter = 'k' println("$letter") }
When running the program, the output is:
k
In Java, you can perform the following operation:
char letter = 65;
However, the following code results in an error in Kotlin.
var letter: Char = 65 // Error
The Boolean data type has two possible values: true or false.
fun main(args: Array<String>) { val flag = true println("$flag") }
Boolean values are used in decision statements (which will be discussed in later chapters).
An array is a container that holds a collection of data of a single type (values). For example, you can create an array that can hold10An array of 0 Int type values.
In Kotlin, arrays are represented by the Array class. This class has get and set functions, size properties, and other useful member functions.
In Kotlin, strings are represented by the String class. Strings like 'this is a string' are implemented as instances of this class.