What is Kotlin Programming Language ?

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.

Functional programming style
Kotlin relaxes Java’s restriction of only allowing static methods and variables inside a class body. Static objects and functions can be defined at the top level of the package without needing a redundant class level. For compatibility with Java, Kotlin provides a JvmName annotation which specifies a class name used when the package is viewed from a Java project. For example, @file:JvmName(“JavaClassName”).

Main entry point
As in C and C++, the entry point to a Kotlin program is a function named “main”, which is passed an array containing any command line arguments. Perl and Unix/Linux shell script-style string interpolation is supported. Type inference is also supported.

// Hello, World! example
fun main(args: Array<String>) {
val scope = “World”
println(“Hello, $scope!”)
}

Extension methods

Similar to C#, Kotlin allows a user to add methods to any class without the formalities of creating a derived class with new methods. Instead, Kotlin adds the concept of an extension method which allows a function to be “glued” onto the public method list of any class without being formally placed inside of the class. In other words, an extension method is a helper method that has access to all the public interface of a class which it can use to create a new method interface to a target class and this method will appear exactly like a method of the class, appearing as part of intellisense inspection of class methods. For example:

package MyStringExtensions

fun String.lastChar(): Char = get(length – 1)

>>> println(“Kotlin”.lastChar())

Unpack arguments with spread operator
Similar to Python, the spread operator asterisk (*) unpacks an array’s contents as comma-separated arguments to a function:

fun main(args: Array<String>) {
val list = listOf(“args: “, *args)
println(list)
}

Deconstructor methods
A deconstructor’s job is to decompose a class object into a tuple of elemental objects. For example a 2D coordinate class might be deconstructed into a tuple of integer x and integer y. (Note: This feature is NOT to be confused with the similarly sounding destructor method that is common in object oriented programming).

For Example, the collection object contains a deconstructor method that splits each collection item into an index and an element variable:

for ((index, element) in collection.withIndex()) {
println(“$index: $element”)
}

Source:WIKI


Discover more from mycodetips

Subscribe to get the latest posts sent to your email.

Discover more from mycodetips

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top