Java has been around for a long while now, and although I do enjoy coding in Java, today I want to take you through a more modern programming language you can use in place of Java — Kotlin. Personally, I encourage you to venture into Kotlin and experience the difference between the two. This article will introduce you to Kotlin and provide you with a firm foundation to move on and start coding in Kotlin.
What is Kotlin?
Kotlin is a general-purpose, open source, statically typed programming language for the Java virtual machine (JVM), Native, JavaScript and Android that combines object-oriented and functional programming features.
The Kotlin project, which was developed by JetBrains, started in 2010 and was made open source very early on. In addition to the core team of developers at JetBrains, there are also over 100 external contributors on GitHub.
Why is Kotlin good?
Kotlin is free. It is developed under the Apache 2.0 license, and the source code is available on GitHub.
Kotlin offers both object-oriented and functional constructs. Its syntax is familiar to any programmer coming from the object-oriented programming domain, and is pretty much easy to understand from the get-go. Therefore, you can make use of both object-oriented and functional programming styles, or mix elements of the two.
Kotlin is 100% interoperable with the Java programming language. A major emphasis has been placed on ensuring that your existing collection of source code can interact with Kotlin without any issues. It is easy to call Kotlin code from Java, and Java code from Kotlin. Moreover, all your Java frameworks are still available. In addition, there exists an automated Java-to-Kotlin converter built into the integrated development environment (IDE) that simplifies migration of existing code.
Kotlin is a programming language that can be used for Android development, server-side development, web development and desktop development.
Kotlin is supported by the major IDEs, including Android Studio, Eclipse, IntelliJ IDEA, and NetBeans.
Kotlin is an easy language to learn. Kotlin is inspired by some common languages, such as Java, C# and JavaScript. Within a matter of days, you can be reading and writing Kotlin code.
Kotlin is more concise than the Java programming language. It is estimated that with Kotlin, you get a 40% reduction in the number of lines of code.
Installation & Setup
Step 1 – Java JDK installation
Step 2 – IDE Installation (IntelliJ IDEA is what I typically use, however, any of the abovementioned IDEs will do)
Step 3 – Open your IDE and create a new Kotlin project
Variables
In Kotlin, use ‘val’ to declare a constant, or ‘var’ keywords to declare a variable. You can specify a type such as String or Double after the variable name.
val fName: String = "Fred" val lName = "Sweet" // will still compile
val name = "Frederick" name = "Peter" // cannot be changed var car = "Toyota" car = "Mercedes" // can be changed
Basic Types
fun main(args: Array) { val a: Int = 10000 val d: Double = 100.00 val f: Float = 100.00f val l: Long = 1000000004 val s: Short = 10 val b: Byte = 1 val letter: Char = 'A' //char val bool: Boolean = true //boolean val string :String = "I am a String!" //string val numbers: IntArray = intArrayOf(1, 2, 3, 4, 5) //int array }
Control Flow
// If - Else fun main(args: Array) { val a:Int = 6 val b:Int = 3 var max: Int if (a > b) { max = a } else { max = b } print("Maximum of a and b is " +max) }
//For Loop fun main(args: Array) { val items = listOf(1, 2, 3, 4) for (i in items) println("values of the array"+i) }
//Use of When fun main(args: Array) { val x:Int = 7 when (x) { 1 -> print("x = = 1") 2 -> print("x = = 2") else -> { print("x is neither 1 nor 2") } } }
Object & Class
class myClass { private var name: String = "Frederick" fun printMe() { print("Hello "+name) } } fun main(args: Array) { val obj = myClass() // create obj object of myClass class obj.printMe() }
Functions
fun main(args: Array) { println(MyFunction("Sweetcode")) } fun MyFunction(x: String): String { var c:String = "Hey!! Welcome To --" return (c+x) }
Safety Features
Kotlin was designed to eliminate the danger of null pointer references, and to streamline the handling of null values.
For example, a regular variable of type String cannot hold null:
var a : String ="abc" a = null // compilation error
If you need to allow nulls, you can declare a nullable type by appending a question mark to the type.
var b: String? ="abc" b = null // ok
And that’s all for now. Thank you for reading! I hope you’ve been convinced to start coding in Kotlin.