What is Kotlin?
Kotlin is a programming language introduced by JetBrains in 2011, the official designer of the most intelligent Java IDE, named Intellij IDEA. Kotlin is free, has been free, and will remain free. It is developed under the Apache 2.0 license and the source code is available on GitHub.
Why Kotlin?
Kotlin is getting high popularity among all levels of programmers and it is used for:
Cross-platform Mobile applications.
Android Application Development.
Web Application Development
Server Side Applications
Desktop Application Development
Data science-based applications
First Code
fun main(args: Array<String>)
{
println("Hello, world!")
}
Output:
Hello, world!
Keywords in Kotlin
as | as? | break | class |
continue | do | else | false |
for | fun | if | in |
!in | interface | object | is |
!is | null | package | return |
super | this | throw | true |
try | type | alias | type |
of | val | var | when |
while | | | |
Kotlin - Variables
Syntax
Following is a simple syntax to create two variables and then assign them different values
var name ="Abhay"
var age =21
var height =6.2
Examples
Once a variable is created and assigned a value, later we can access its value using its name as follows:
funmain()
{
var name ="Abhay"
var age =21
println(name)
println(age)
}
output:
Abhay
21
Kotlin Read-only Variables
A read-only variable can be declared using val (instead of var) and once a value is assigned, it can not be re-assigned.
fun main()
{
val name ="Abhay"
val age =19
println("Name = $name")
println("Age = $age")
name ="Abhay Tiwari"// Not allowed, throws an exception
age =11
println("Name = $name")
println("Age = $age")
}
output:
main.kt:8:4: error: val cannot be reassigned
name = "Abhay Tiwari" // Not allowed, throws an exception
^
main.kt:9:4: error: val cannot be reassigned
age = 11
^
Hope you understand the introduction to kotlin in the next blog we are going to learn data types in kotlin.
Thank you
The journey of solving bug and completing project on time in Kotlin can be challenging and lonely. If you need help regarding other sides to Kotlin, we’re here for you!
Comments