The arithmetic operators supported in Kotlin include +
addition, -
subtraction, *
multiplication, /
division, and %
modulus.
5 + 7 // 129 - 2 // 78 * 4 // 3225 / 5 // 531 % 2 // 1
The order of operations for compound arithmetic expressions is as follows:
When an expression contains operations such as multiplication and division or addition and subtraction side by side, the compiler will evaluate the expression in a left to right order.
5 + 8 * 2 / 4 - 3 // 63 + (4 + 4) / 2 // 74 * 2 + 1 * 7 // 153 + 18 / 2 * 1 // 126 - 3 % 2 + 2 // 7
An augmented assignment operator includes a single arithmetic and assignment operator used to calculate and reassign a value in one step.
var batteryPercentage = 80// Long SyntaxbatteryPercentage = batteryPercantage + 10// Short Syntax with an Augmented Assignment OperatorbatteryPercentage += 10
Increment and decrement operators provide a shorthand syntax for adding or subtracting 1
from a value. An increment operator consists of two consecutive plus symbols, ++
, meanwhile a decrement operator consists of two consecutive minus symbols, --
.
var year = 2019year++ // 2020year-- // 2019
The Math
library, inherited from Java, contains various mathematical functions that can be used within a Kotlin program.
Math.pow(2.0, 3.0) // 8.0Math.min(6, 9) // 6Math.max(10, 12) // 12Math.round(13.7) // 14
A mutable variable is declared with the var
keyword and represents a value that is expected to change throughout a program.
var age = 25age = 26
An immutable variable is declared with the val
keyword and represents a value that must remain constant throughout a program.
val goldenRatio = 1.618
When a data type is not specified in a variable declaration, the variable’s data type can be inferred through type inference.
// The following variable is assigned a text value within double quotes, thus the inferred type is Stringvar color = "Purple"
String concatenation is the process of combining Strings using the +
operator.
var streetAddress = "123 Main St."var cityState = "Brooklyn, NY"println(streetAddress + " " + cityState)// Prints: 123 Main St. Brooklyn, NY
String templates contain String values along with variables or expressions preceded by a $
symbol.
var address = "123 Main St. Brooklyn, NY"println("The address is $address")// Prints: The address is 123 Main St. Brooklyn, NY
The Kotlin String and Character data types contain various built-in properties and functions. The length
property returns the number of characters in a String, and the capitalize()
function capitalizes the first letter of a String.
var monument = "the Statue of Liberty"println(monument.capitalize()) // Prints: The Statue of Libertyprintln(monument.length) // Prints: 21
Character escape sequences consist of a backslash and character and are used to format text.
\n
Inserts a new line\t
Inserts a tab\r
Inserts a carriage return \'
Inserts a single quote \"
Inserts a double quote\\
Inserts a backslash\$
Inserts the dollar symbolprint("\"Excellent!\" I cried. \"Elementary,\" said he.")// Prints: "Excellent!" I cried. "Elementary," said he.