0b1100
12
0xC
Int
literal can be assigned to a Byte
or Short
variable without getting a compile-time error if the value is within range.Int
type (unless suffixed by L
).Long
literal can be obtained by appending the suffix L
to an integer literal. E.g. 34L
has a Long
type.3.134
2.1e10
, 3e-7
Double
by default.Float
literal, add a suffix ‘f’ or ‘F’. E.g. 3.14F
has a Float
type.2+3*4
, multiplication is performed before addition since the * operator has higher precedence than +.1-3-4
will be parsed by the Kotlin compiler as if we had written (1-3)-4
due to the fact that the minus operator is left-associative.Type | Size in Bits | Sample Operators | Literals |
---|---|---|---|
Boolean | unspecified | && || ! | true false |
Byte | 8 | + \(-\) * / % | |
Char | 16 | + | ‘a’ ‘0’ ‘\\’ |
Short | 16 | + \(-\) * / % | |
Int | 32 | + \(-\) * / % | 321 0x3A 0b11 |
Long | 64 | + \(-\) * / % | 3L |
Float | 32 | + \(-\) * / % | 2F 14.2e23f |
Double | 64 | + \(-\) * / % | 3.21 14.2e23 |
String | + | “Hello” | |
Array | [] |
Here is a list of the hard keywords
as as? break class continue do else false for fun
if in !in interface is !is null object package return
super this throw true try typealias val var when while
There are other kinds of keywords. We may encounter some of them later.
ticketNumber
BigInteger
MAX_LINE_LENGTH
An assignment statement starts with name of the variable, followed by the = operator, followed by an expression. E.g.
a = 2 * b + 6
Thus, the variable name appearing on the LHS denotes memory location while any variable name appearing on the RHS denotes value.
val
.Example declaration and usage:
val c : Char = 'A'
val digit : Char = '9'
val greeting : String = "Hi"
val person : String = "Alice"
println(greeting + " " + person)
var
.Example declaration, usage, and reassignment:
var n : Int = 1
var f : Float = 0F
var d : Double = 2.1
f = f * 3.1F
d = 3e-2
var greet : String = "Hello!"
greet = "Hi"
In the simplest case, an expression consists of literals, variables, and function calls joined together by operators.
b * 2 / 3 + kotlin.math.sin(4)
There are other kinds of expressions. We’ll come to them soon.
In an arithmetic expression involving mixed types of numeric operands, Kotlin has overloaded the operators so it seems as if to convert the values in the following direction to make both operands have the same type as the “biggest” type.
Byte → Short → Int → Long → Float → Double
Examples:
3 + 4.2 // value: 7.2, type: Double
1 + 7L - 2 // value: 6L, type: Long
The user can also explicitly requests the compiler to convert a value of one type into another type. E.g.
val a : Int = 2
val b : Byte = (a + 3).toByte()
val s : Short = 4.toShort()
toBoolean()
toChar()
toByte()
toShort()
toInt()
toLong()
toFloat()
toDouble()
Example usage:
"TrUe".toBoolean() ⇒ true
66.toChar() ⇒ 'B'
25.toByte() ⇒ a Byte type with value of twenty-five
265.toByte() ⇒ a Byte type with value of nine
print()
— outputs its argument.println()
— outputs its argument and a newline, and flushes the output buffer.print()
and println()
functions take exactly one argument. Their argument can be of any type, and it is automatically converted to the String type (if necessary) before being output.abs
— for Double, Float, Int, and Longmin
, max
— for Double, Float, Int, and Longfloor
, ceil
, round
, truncate
— for Double and Floatsin
, cos
, tan
— for Double and Floatexp
, ln
, log10
, log2
, log
— for Double and Floatpow
— for Double and Floatsqrt
— for Double and FloatroundToInt
, roundToLong
— for Double and FloatE
— the famous \(e\) (the base of natural logarithm)PI
— \(\pi\) (the circle constant: circumference \(\div\) diameter)/*
* This program expects one command line argument n as a positive integer.
* Firstly, it generates a random integer (in the range of Int) and prints it.
* It then generates a random real in the range [0,1) and prints it.
* Lastly, it generates a random integer between 0 and n-1 inclusive
* and prints it.
*/
fun main(args: Array<String>) {
val randGen = java.util.Random()
val n = args[0].toInt()
// a pseudo-random integer
var k = randGen.nextInt()
println("Your random integer is: $k")
// a pseudo-random real between 0.0 and 1.0
val r = randGen.nextDouble()
println("Your random real number in [0, 1) is: $r")
// a pseudo-random integer between 0 and n-1
k = randGen.nextInt(n)
println("Your random integer between 0 and ${n-1} inclusive is: $k")
}