Math.pow(2.0, 3.0) // 8.0
Math.min(6, 9) // 6
Math.max(10, 12) // 12
Math. round(13.7) // 14
Data Types and Variables
Math library
Increment and decrement operators
var year = 2019
year++ // 2020
year-- // 2019
Enhanced assignment operator
var batteryPercentage = 80
// long syntax
batteryPercentage = batteryPercantage + 10
// short syntax with augmented assignment operator
batteryPercentage += 10
Order of operations
5 + 8 *2 /4 -3 // 6
3 + (4 + 4) /2 // 7
4 *2 + 1 *7 // 15
3 + 18 /2 *1 // 12
6 -3 % 2 + 2 // 7
Arithmetic Operators
5 + 7 // 12
9 -2 // 7
8 *4 // 32
25 /5 // 5
31 % 2 // 1
+
addition, -
subtraction, *
multiplication, /
division, and %
modulus
Character escape
print("\"Excellent!\" I cried. \"Elementary,\" said he.")
// Print: "Excellent!" I cried. "Elementary," said he.
\n
insert new line\t
inserts a tab\r
inserts carriage return\'
inserts a single quote\"
inserts a double quote\\
inserts a backslash\$
inserts a dollar sign
Built-in Properties and Functions
var monument = "the Statue of Liberty"
println(monument. capitalize())
// print: The Statue of Liberty
println(monument. length)
// print: 21
String Templates
var address = "123 Main St."
println("The address is $address")
// prints: The address is 123 Main St.
String concatenation
var streetAddress = "123 Main St."
var cityState = "Brooklyn, NY"
println(streetAddress + " " + cityState)
// Print: 123 Main St. Brooklyn, NY
Type inference
// The following variables are assigned a literal value inside double quotes
// so the inferred type is String
var color = "Purple"
Immutable variables
val goldenRatio = 1.618
Mutable variables
var age = 25
age = 26
Comments