Introduction
Kotlin is a programming language that commonly used in many cases including mobile application development for Android, web application development and others.
Setup
Before using Kotlin, make sure the Java is installed. After the Java is installed, write Kotlin codes using specific IDE called IntelliJ IDEA Community Edition that can be downloaded here.
Writing the First Code
To create the first Kotlin program, Open the IntelliJ IDEA and follow these steps:
- Create new project by choosing Kotlin. Then specify the project's name.
- Specify the project's location.
- For project template, choose console application.
- For build system, choose Gradle Kotlin.
- For project JDK, choose the Java JDK that already installed. Then, click Next.
- Confirm the project's specification then click Finish.
Those steps is illustrated in this screenshot.
After the project is created, write this code.
fun main (args: Array<String>) {
println("Hello World!")
}
This is the output after the code is executed.
Hello World!
Based on the code above, the 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.
Variables
This is the basic syntax to create a variable in Kotlin.
// with data type
var variable_name: data_type = value
// without data type
var variable_name = value
This is the basic syntax to create a variable that the value can be assigned only once using 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
This is the basic syntax to create a constant in Kotlin. Constant is a variable that the value cannot be changed.
// available only in global scope
const val variable_name = value
If the value of variable is not initialised. The data type must be specified.
This is the example of creating variables in Kotlin.
// 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")
}
Output
App Name: MyKotlinApp
The area of circle: 153.86
Based on the code above, some variables that already created, the data types can be specified or not. Some variables like 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.
There is another variable in Kotlin called lateinit var
. This variable is useful to create a variable that the value is not directly initialised or will be initialised in specific conditions.
This is the basic syntax of creating lateinit var
variable.
// The data type must be specified
lateinit var var_name: data_type
Nullable Values
In Kotlin, the nullable values is annotated by ?
notation after the variable's data type. The nullable values notation can be used for variable that could contains null value.
In this example, the 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")
}
Output
index: null
Based on the code above, the code is still executed although the value is null at name
variable. This happens because the ?
notation is used to specify that the certain value is nullable.
Data Types
This is the list of commonly used data types in Kotlin.
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) | - | - |
Operators and Modifiers
In Kotlin, there many arithmetic operators that can be used. The arithmetic operation must be done with the same data type.
Operator | Description |
---|---|
+ |
add operation |
- |
substract operation |
* |
multiply operation |
/ |
division operation |
% |
modulo operation (get the remainder from division operation) |
This is the example of arithmetic operations in Kotlin.
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}")
}
Output
a + b = 14
a - b = 4
a * b = 45
a / b = 1
a % b = 4
Based on the code above, the arithmetic operations is wrapped inside ${}
notation to prints out the operation's result.
Kotlin has similiar modifiers with Java programming language. This is the list of modifiers that available in Kotlin.
Modifier | Own Class | Sub Class | Package | Outside Package |
---|---|---|---|---|
public | ✔ | ✔ | ✔ | ✔ |
protected | ✔ | ✔ | ||
private | ✔ | |||
internal | ✔ | ✔ |
The check mark (✔) represents the visibility.
By default, the modifier is public. The modifier can be added before declaration.
// add modifier into function
internal fun randomizer() {}
// add modifier into variable
private val name: String = "test"
Type Conversions and Type Checking
In Kotlin the type conversion can be done by using 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
For numeric values, the type conversion can be done by using the data type's function. For example to convert into Double
data type use toDouble()
.
// "to" followed with numeric data type
toDouble()
This is the example of type conversion for numeric values.
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")
}
Output
Int value: 24
Float value: 24.0
Double value: 24.0
Based on the code above, the toFloat()
function is used to convert into Float
data type and the toDouble()
function is used to convert into Double
data type.
To check the data type of variable, use is
keyword followed with the data type that wants to be checked.
variable_name is data_type
In this example, this code is used to check if num
variable is an Int
.
fun main() {
val num = 25
if (num is Int) {
println("num is an integer")
}
}
Output
num is an integer
Sources
Learn more about Kotlin's basic syntax in this link.
Learn more about Kotlin's supported data types in this link.
Learn more about Kotlin's type conversion or casting in this link.
Learn more about Kotlin's supported modifiers in this link.
I hope this article is helpful for learning the Kotlin programming language. If you have any thoughts or comments you can write in the discussion section below.
Top comments (0)