Kotlin is a statically typed programming language that runs on the Java virtual machine and also can be compiled to JavaScript source code or use the LLVM compiler infrastructure. Its primary development is from a team of JetBrains programmers based in Saint Petersburg, Russia.
Kotlin comes from the good folks at JetBrains, the guys behind the best IDEs like IntelliJ and our beloved Android Studio. They understand the pain we developers face in our day-to-day development workflow and are trying to address the real use cases in this language.
Most interestingly You can start using Kotlin in your existing project with all your old Java code straight away.
We will discuss Java vs. Kotlin in our next blog, before that let's see a few interesting concepts that are required for you to understand Kotlin.
Like other programming languages in Kotlin, main function is a must. That is where the compiler starts to compile the program. In Kotlin the main function can be written as follows:
Variables can be declared using two keywords namely val and var.
i. val: It is used for variables whose value never changes.
var: It is used for a variable whose
value can change.
Syntax would be,
Let's see example So you will get idea.
Example of val, Which gives error while you will try to reassign it.
val number: Int = 10
fun main() {
println(number) // 10
number = 20
println(number) // Error val cannot be reassigned
}
Example of var, you can reassign value of it.
var number: Int = 10
fun main() {
println(number) //10
number = 20
println(number)
//20
}
In general, var is called read-write reference and val is called a read-only reference. We will not be able to reassign the value but we can modify the properties.
Type Inference
Type inference refers to automatically detect the data type of the variable during the compilation time. In Kotlin the type is defined after variable name:
If you assign a type for a variable then you cannot reassign a different type for the same variable by doing so you will get an error,
However, using the keyword “Any” you can assign an Int to a String or vice-versa as follows:
Kotlin variables cannot hold null variables by default so in order to hold null variables you have to use a different syntax as shown below, all you have to do is you have to add a “?” in front of the variable declaration.
var number: Int? = null
fun main()
{
println(number) // null
}
In case if you fail to do so, you will get an error saying “Null can not be a value of non-null type int”.
var num: Int = 10
fun main() {
if(num == 10)
{
\nnum = num + 10
println(num) //20
}
else
{
num = 10
println(num)
}
}
Functions are a block of organized and reusable code that is used to perform a single task. In Kotlin in order to create a function you have to use the fun keyword, and next defining the data types of input that the function takes, and then finally you have to return the output of the function.
The statement 1, 2 and n are just executable statements or also called the body of the function which must be placed within the function.
fun add_two_numbers(): Int{
val num1: Int =
10
val num2: Int = 20
val num3: Int = num1 + num2
return num3
}
fun main() {
println(add_two_numbers())
}
In Kotlin in order to declare a class you need to use the keyword class and then its properties.Classes represent state using properties. A property is a class-level variable that can include a getter, a setter, and a backing field. An object is an instance of a class, with the help of an object we can access the variables of the class.
class dog {
var
name: String = "Danny"
var breed: String = "Boxer"
var color: String = "White"
}
fun main() {
var d
= dog()
println(d.name) //Danny
println(d.breed) //Boxer
println(d.color)
//White
}
In order to get the input from the user, Kotlin provides a special method called readline which reads the line entered by the user.
fun main()
{
var name: String? = null
println("Neerwebapp")
name = readLine()
println("Your name is: $name") //
Neerwebapp
}
The above are some basic and important concepts of Kotlin programming that you need to know. However, this is not the end of Kotlin, this is just the beginning. I will try to upload more tutorials about Kotlin Programming and help you guys in mastering the language.