Kotlin, with Java, shares core language elements such as classes, objects, interfaces, and many common data types. However, Kotlin introduces some differences, such as nullable types, which are not present in Java. Additionally, Kotlin incorporates functional programming aspects.
Data types
Kotlin supports data types of Numbers (byte, short, int, long, float, double), booleans (true or false), characters, strings, and arrays. Like Java and C++, Kotlin is statically typed, so type correctness of any variables and expressions is determined at compile time, with compiler catching type errors and can subsequently prevent the code from running. Kotlin can infer type of any variable using Type Inference i.e. without explicit declaration, from the assigned value format. Despite its advantages for code readbaility and conciseness and common use in Kotlin, type inference is not always best approach. Explicity type decalration is sometime best option, within specific context of the code.
Variable declaration
Variables are used to store values for a specific data type. For mutable types, key word var is used and for immutable types, either val (compile time values) or const (run type values) is used.
var: Int = 2 or, simply using Kotlin's type inference var = 2 var can be assigned different value (read-write). val: String = "This is Kotlin tutorial" or, using type inference val = "This is Kotlin tutorial" val here, is immutable (read-only), equivalent to, as final in Java. val flag:booleanValue = true val pi: Double = 3.14 val charValue: Char = 'A' val intArray: Array= arrayOf(1, 2, 3, 4, 5,6) val list: List = listOf(1, 2, 3, 4, 5,6) val set: Set = setOf("apple", "carrot", "orange") val map: Map = mapOf(1 to "one", 2 to "two", 3 to "three") val intRange: IntRange = 1..15 val charRange: CharRange = 'A'..'Z'
All above with explicit data type can be declared by removing it, using type inference, where appropriate.
Note above that Kotlin statements does not require a semicolon (;) to end the statement like many other programming languages, such as Java, C++, etc.
Comments
// This is single line Kotlin comment /* This is * multi-line Kotlin comment */
Functions
Functions - Functions or Methods in Kotlin are declared by key word fun.
Main Function - This function declared as fun main() is the entry point for a Kotlin program.
fun main() { println("Kotlin Tutorial at www.zyasin.com!") }
Top-Level Function - These are directly defined in the file and are not tied to any specific class or instance. Above defined 'fun main()' is an example of a
top-level function in Kotlin. They encapsulate functionality not tied to a specific class, and generally used for utility functions, helper functions or entry point
of a program. They improve code readability and maintability.
Functions as Expressions
-
val function: () -> Unit = { println("Kotlin Function!") }
Function that takes no arguments and returns Unit (as void in Java). The right-hand side of the assignment = { println("Kotlin Function !") } is a lambda expression. -
val functionWithParams: (Int, Int) -> Int = { a, b -> a + b }
Right hand side is again a lambda expression.
val result = functionWithParams(5, 4) // result will be 9
Class
To define a Kotlin class use class key word, followed by class name.
class ClassName { //declare properties (using key words val or var.) //declare functions (using key word fun.) }
Default Kotlin classes are final and can't be extended unless declared with open key word.
Kotlin provides visibility modifiers like private, protected, internal, and public to control the visibility of class members. By default, class members are public.
Abstract Class
For an abstract class in Kotlin, no instance can be created. It is open by default (can't be final!) and can be sub-classed by other non-abstract classes to provide the desired functionality. Abstract methods and fields of abstract class are declared with abstract key word and not final by default, so can be overriden in the implementing class. Any non-abstract members of an abstract class are final by default, which can be only be overriden in child class, once declared as open.
Constructor
For proper object initialization, a Kotlin class can have Primary and Secondary constructors.
Primary Constructor: It is the main way to initialize the properties of a class, defined as part of the class header and responsible for initializing the properties when an object of class is created. To declare a primary constructor keyword constructor can be optionally added just after the class name. The primary constructor defined with the class header do not contain any code. Initialization code is placed within initializer blocks, inside the class body prefixed with the init keyword.Secondary Constructor:class ClassName(parameters) { //declare properties (using key words val or var.) // initializer blocks }
A class can have none, one or more secondary constructors. The secondary constructors are created using the constructor keyword. They allow initialization of variables and can provide some logic to the class. One common use of secondary constructor is when extending a class providing multiple cinstructors which each initializing the class in a different way. Compiler decides which constructor to pick based on arguments.
Here the Student class has a secondary constructor that takes only firstName and lastName parameters. The secondary constructor delegates to the primary constructor using the this keyword and provides a default value of 0 for the studentId parameter. This allows to create Student objects without specifying a student ID.class Student(val studentId: Int, val firstName: String, val lastName: String) { val fullName: String init { fullName = "$firstName $lastName" } // Secondary costructor constructor(firstName: String, lastName: String) : this(0, firstName, lastName) }
Instance
To define a Kotlin instance or object, call the constructor of class like a regular function i.e. for class Chair, an instance can be declared as var chair = Chair(). Properties and function of a class can be accessed using . notation i.e. name of instance followed by dot and some property or function of class.
Interface
In Kotlin, interfaces can contain method implementation as well as abstract methods and properties declaration. An interface needs to be implemented by a class in order for any use. In Kotlin interfaces are not implicitly final. Interfaces can be implemented by any class without any restrictions. However, interface can be forced to be final by using "final" key word before them, which then prevent other classes from implementing it or inheriting from it, however it is extremely rare cases where such need can arise.
To implement above interface in a class:interface KotlinInterface { var someString: String // abstract property fun someFunction() // abstract method fun string() = "Some string" // method with default implementation }
class ImplementKotlinInterface : interface KotlinInterface { override var someString: String = "some string" // property declared in interface implemented override fun someFunction() = "display string outpt" // function declared in interface implemented override fun string() = "Some string text overriden" // text in method of interfce overridden }
Control Flow
val x = 14 if (x > 14) { println("x is greater than 14") } else if (x == 14) { println("x is equal to 14") } else { println("x is less than 14") }
val x = 3 when (x) { 1 -> println("x is 1") 2 -> println("x is 2") else -> println("x is neither 1 nor 2") }
for (i in 1..5) { println(i) }
fun isPositive(number: Int): Boolean { if (number > 0) { return true } else { return false } }
for (i in 1..10) { if (i == 5) { break } println(i) }
for (i in 1..5) { if (i == 3) { continue } println(i) }