Kot Lin Notes
Kot Lin Notes
It has become very popular since it is compatible with Java (one of the most
popular programming languages out there), which means that Java code
(and libraries) can be used in Kotlin programs.
Kotlin Syntax
In the previous chapter, we created a Kotlin file called Main.kt, and we used
the following code to print "Hello World" to the screen:
Example
fun main() {
println("Hello World")
}
Example explained
The fun keyword is used to declare a function. A function is a block of code
designed to perform a particular task. In the example above, it declares
the main() function.
The main() function is something you will see in every Kotlin program. This
function is used to execute code. Any code inside the main() function's curly
brackets {} will be executed.
For example, the println() function is inside the main() function, meaning that
this will be executed. The println() function is used to output/print text, and
in our example it will output "Hello World".
Kotlin Variable
Variable refers to a memory location. It is used to store data. The data of variable can
be changed and reused depending on condition or on information passed to the
program.
Variable Declaration
Kotlin variable is declared using keyword var and val.
The difference between var and val is specified later on this page.
Here, variable language is String type and variable salary is Int type. We don't require
specifying the type of variable explicitly. Kotlin complier knows this by initilizer
expression ("Java" is a String and 30000 is an Int value). This is called type inference
in programming.
We can also explicitly specify the type of variable while declaring it.
1. var language: String ="Java"
2. val salary: Int = 30000
It is not necessary to initialize variable at the time of its declaration. Variable can be
initialized later on when the program is executed.
Example
Here, the value of variable salary can be changed (from 30000 to 40000) because
variable salary is declared using var keyword.
Here, we cannot re-assign the variable language from "Java" to "Kotlin" because the
variable is declared using val keyword.
Kotlin Data Type
Data type (basic type) refers to type and size of data associated with variables and
functions. Data type is used for declaration of memory location of variable which
determines the features of data.
In Kotlin, everything is an object, which means we can call member function and
properties on any variable.
o Number
o Character
o Boolean
o Array
o String
Number Types
Number types of data are those which hold only number type data variables. It is
further categorized into different Integer and Floating point.
Example
Example
Array
Arrays in Kotlin are represented by the Array class. Arrays are created using library
function arrayOf() and Array() constructor. Array has get (), set() function, size
property as well as some other useful member functions.
The elements of array are accessed through their index values (array[index]). Array
index are start from zero.
1. val id = arrayOf(1,2,3,4,5)
2. val firstId = id[0]
3. val lasted = id[id.size-1]
String
String in Kotlin is represented by String class. String is immutable, which means we
cannot change the elements in String.
String declaration:
Types of String
String are categorize into two types. These are:
1. Escaped String: Escape String is declared within double quote (" ") and may
contain escape characters like '\n', '\t', '\b' etc.
2. Raw String: Row String is declared within triple quote (""" """). It provides facility
to declare String in new lines and contain multiple lines. Row String cannot contain
any escape character.
In Java
1. int value1 = 10;
2. long value2 = value1; //Valid code
In Kotlin
1. var value1 = 10
2. val value2: Long = value1 //Compile error, type mismatch
1. var value1 = 10
2. val value2: Long = value1.toLong()
The list of helper functions used for numeric conversion in Kotlin is given below:
o toByte()
o toShort()
o toInt()
o toLong()
o toFloat()
o toDouble()
o toChar()
We can also converse from larger data type to smaller data type.
What you'll do
Create a new Android project and a default app called HelloWorld.
Create an emulator (a virtual device) so you can run your app on your computer.
Run the HelloWorld app on virtual and physical devices.
Explore the project layout.
Explore the AndroidManifest.xml file.