38
loading...
This website collects cookies to deliver better user experience
fun main (args: Array<String>) {
println("Hello World!")
}
Hello World!
println
function is executed inside main
function that prints out the Hello World!
to the console. Notice that the semicolon (;
) is not required when writing the Kotlin codes.// with data type
var variable_name: data_type = value
// without data type
var variable_name = value
val
. When using val
, the variable becomes immutable variable.// with data type
val variable_name: data_type = value
// without data type
val variable_name = value
// available only in global scope
const val variable_name = value
If the value of variable is not initialised. The data type must be specified.
// create a constant that available in global scope
const val APPLICATION_NAME: String = "MyKotlinApp"
fun main() {
// create a constant that only available in main function
val PI: Double = 3.14
// create a variable called radius
val radius: Double
// initiate the value in radius variable
radius = 7.0
// create a variable called area
var area = 0.0
// assign the calculation result into area variable
area = PI * radius * radius
// print out the result
println("App Name: $APPLICATION_NAME")
println("The area of circle: $area")
}
App Name: MyKotlinApp
The area of circle: 153.86
PI
, radius
and APPLICATION_NAME
have the specified data types. The area
variable's data type is not specified but the value has to be assigned. Notice that inside println()
function the variable's value is printed using $
(dollar sign) followed with the variable's name.lateinit var
. This variable is useful to create a variable that the value is not directly initialised or will be initialised in specific conditions.lateinit var
variable.// The data type must be specified
lateinit var var_name: data_type
?
notation after the variable's data type. The nullable values notation can be used for variable that could contains null value.name
variable is a nullable value.fun main() {
// added into variable
val name: String? = null
// use ? notation to access the variable's function
val index = name?.indexOf("z")
// prints out the index
println("index: $index")
}
index: null
name
variable. This happens because the ?
notation is used to specify that the certain value is nullable.Data Type | Value | Minimum Value | Maximum Value |
---|---|---|---|
Byte | non-decimal number | -128 | 127 |
Short | non-decimal number | -32768 | 32767 |
Int | non-decimal number | -2147483648 | 2147483647 |
Long | non-decimal number | -9223372036854775808 | 9223372036854775807 |
Float | decimal or floating number | 1.4E-45 | 3.4028235E38 |
Double | decimal or floating number | 4.9E-324 | 1.7976931348623157E308 |
Boolean |
true or false
|
- | - |
Char | single character (alphabetic) | - | - |
Operator | Description |
---|---|
+ |
add operation |
- |
substract operation |
* |
multiply operation |
/ |
division operation |
% |
modulo operation (get the remainder from division operation) |
fun main() {
val a = 9
val b = 5
println("a + b = ${a + b}")
println("a - b = ${a - b}")
println("a * b = ${a * b}")
println("a / b = ${a / b}")
println("a % b = ${a % b}")
}
a + b = 14
a - b = 4
a * b = 45
a / b = 1
a % b = 4
${}
notation to prints out the operation's result.Modifier | Own Class | Sub Class | Package | Outside Package |
---|---|---|---|---|
public | ✔ | ✔ | ✔ | ✔ |
protected | ✔ | ✔ | ||
private | ✔ | |||
internal | ✔ | ✔ |
// add modifier into function
internal fun randomizer() {}
// add modifier into variable
private val name: String = "test"
as
keyword. The unsafe conversion throws exception if the conversion fail. The safe conversion returns null if conversion fail.// unsafe conversion
val variable_name: target_type = other_variable as target_type
// safe conversion
val variable_name: target_type? = other_variable as? target_type
Double
data type use toDouble()
.// "to" followed with numeric data type
toDouble()
fun main() {
val number = 24
val floatNum: Float = number.toFloat()
val doubleNum: Double = number.toDouble()
println("Int value: $number")
println("Float value: $floatNum")
println("Double value: $doubleNum")
}
Int value: 24
Float value: 24.0
Double value: 24.0
toFloat()
function is used to convert into Float
data type and the toDouble()
function is used to convert into Double
data type.is
keyword followed with the data type that wants to be checked.variable_name is data_type
num
variable is an Int
.fun main() {
val num = 25
if (num is Int) {
println("num is an integer")
}
}
num is an integer