Data types
Kotlin supports data types of Numbers (byte, short, int, long, float, double), booleans (true or false), characters, strings, and arrays. Kotlin is satistically typed (like Java and C++), so type of any variable is known at compile type. Even without explicit declaration Kotlin can infer type of any variable using Type Inference i.e. type of variable can be inferred without explicit declaration, from the assigned value format. Despite advantages for code readbaility and conciseness, type inference is not always best approach. Explicity type decalration is sometime best option, within specific context and goals of code.
Variable types
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.
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
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!") }
Class
Class - To define a Kotlin class use class key work, followed by class name.
class ClassName { //declare properties (using key words val or var.) //declare functions }
Default Kotlin classes are final and can't be extended unless declared with open key word.
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 Kotlin constructor, Kotlin gives us few options. A Kotlin class can have Primary and Secondary constructors.
Primary Constructor: Common way to declare Primary constructor is by keyword constructor just after the class name as class header. In the absence of a modifier or annotation to the primary the constructor keyword is unnecessary. 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 constructor (parameters) { //declare properties (using key words val or var.) //declare functions }
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.
Instance
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.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 }