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. Its primary development is from a team of JetBrains programmers based in Saint Petersburg, Russia. While the syntax is not compatible with Java, Kotlin is designed to interoperate with Java code and is reliant on Java code from the existing Java Class Library, such as the collections framework. Kotlin uses aggressive type inference to determine the type of values and expressions for which type has been left unstated. This reduces language verbosity relative to Java, which demands often entirely redundant type specifications prior to version 10.
As of Android Studio 3.0 (October 2017) Kotlin is a fully supported programming language by Google on the Android Operating System, and is directly included in the Android Studio 3.0 IDE package as an alternative to the standard Java compiler. The Android Kotlin compiler lets the user choose between targeting Java 6- or Java 8-compatible bytecode.
Video Kotlin (programming language)
History
In July 2011 JetBrains unveiled Project Kotlin, a new language for the JVM, which had been under development for a year. JetBrains lead Dmitry Jemerov said that most languages did not have the features they were looking for, with the exception of Scala. However, he cited the slow compile time of Scala as an obvious deficiency. One of the stated goals of Kotlin is to compile as quickly as Java. In February 2012, JetBrains open sourced the project under the Apache 2 license.
The name comes from Kotlin Island, near St. Petersburg. Andrey Breslav mentioned that the team decided to name it after an island just like Java was named after the Indonesian island of Java (though the programming language Java was perhaps named after the coffee.)
JetBrains hopes that the new language will drive IntelliJ IDEA sales.
Kotlin v1.0 was released on February 15, 2016. This is considered to be the first officially stable release and JetBrains has committed to long-term backwards compatibility starting with this version.
At Google I/O 2017, Google announced first-class support for Kotlin on Android.
Kotlin v1.2 was released on November 28, 2017. Sharing Code between JVM and Javascript platforms feature was newly added to this release.
Maps Kotlin (programming language)
Philosophy
Development lead Andrey Breslav has said that Kotlin is designed to be an industrial-strength object-oriented language, and a "better language" than Java, but still be fully interoperable with Java code, allowing companies to make a gradual migration from Java to Kotlin.
Semicolons are optional as a statement terminator; in most cases a newline is sufficient for the compiler to deduce that the statement has ended.
Kotlin variable declarations and parameter lists have the data type come after the variable name (and with a colon separator), similar to Pascal.
Variables in Kotlin can be immutable, declared with the val keyword or mutable, declared with the var keyword.
Class members are public by default, and the classes themselves are sealed by default meaning that creating a derive class is disabled without requiring explicit keywords in the base class to enable it.
In addition to the classes and methods (called member functions in Kotlin) of object-oriented programming, Kotlin also supports procedural programming with the use of functions.
Syntax
Functional Programming Style
Kotlin relaxes Java's restriction of allowing Static Methods and variables inside of only a class body. Instead, Kotlin allows definition of static Objects and Functions at the top-level of the Package body without needing a redundant class level; similar to languages such as C or Pascal. For backwards compatibility with Java, Kotlin adds a special attribute to a Kotlin package body that specifies a Java class name for nesting toplevel static functions and variables when the Kotlin package is viewed from a Java project. (ie. @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.
Extension Methods
Similar to C#, Kotlin allows a user to add methods to any class without the formalities of creating a derived classes with new methods. Instead, Kotlin add 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:
By placing the preceding code in the top-level of a package, the String class is extended to include a lastChar method that was not included in the orignal definition of the String class.
Unpack Arguments with Spread Operator
Similar to Python, the Spread operator asterisk (*) unpacks an array's contents as comma-separated arguments to a function:
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 orient programming).
For Example, the collection object contains a deconstructor method that splits each collection item into an index and an element variable:
Nested Functions
Kotlin allows local functions to be declared inside of other functions or methods.
Classes are final by Default
In Kotlin if you want to derive a new class from a base class type, then this class needs to be explicitly marked as "open" in order to allow this to happen. This is in contrast to most object oriented languages such as Java where classes are open by default.
Example of a base class that is open to deriving a new subclass from it.
Abstract Classes are Open by Default
Abstract classes define abstract or "Pure Virtual" placeholder function that will be defined in a derived class. Abstract classes are open by default.
Classes are Public by default
Kotlin provides the following keywords to restrict visibility for top-level declaration, such as classes, and for class members:
public, internal, protected, and private.
When applied to a Class Member:
public (default): Visible everywhere internal: Visible in a module protected: Visible in subclasses private: Visible in a class
When applied to a Top-level declaration
public (default): Visible everywhere internal: Visible in a module private: Visible in a file
Example:
Primary Constructor vs. Secondary Constructors
Most classes in Kotlin are created using only Primary constructor syntax which allows only one primary constructor, and the ability to selectively choose which of the constructor properties to initialize based on keyword selection of the property during object creation, and non-initialized constructor properties are simply set to a default value.
However, in cases where more than one constructor is needed for a class, a more general constructor can be used called Secondary Constructor Syntax which closely resembles the constructor syntax used in most object-oriented langues like C++, C#, and Java.
Anko Library
Anko is a library specifically created for Kotlin to help build Android UI applications.
Kotlin Interactive Shell
Kotlin as a scripting Language
Kotlin can also be used as a scripting language. A script is a Kotlin source file (.kts) with top level executable code.
To run a script, we just pass the -script option to the compiler with the corresponding script file.
Kotlin features in a hello world example
Variables in Kotlin can be immutable, declared with the val keyword or mutable, declared with the var keyword.
Kotlin makes a distinction between nullable and non-nullable data types. All nullable objects must be declared with a "?" postfix after the type name. Operations on nullable objects need special care from developers: null-check must be performed before using the value. Kotlin provides null-safe operators to help developers:
- ?. (safe navigation operator) can be used to safely access a method or property of a possibly null object. If the object is null, the method will not be called and the expression evaluates to null.
- ?: (null coalescing operator) often referred to as the Elvis operator:
An example of the use of the safe navigation operator:
Kotlin provides support for higher order functions and Anonymous functions or lambdas.
Lambdas are declared using braces, { } . If a lambda takes parameters, they are declared within the braces and followed by the -> operator.
Tools
- IntelliJ IDEA has plug-in support for Kotlin. IntelliJ IDEA 15 is the first version to bundle the Kotlin plugin in the IntelliJ Installer, and provide Kotlin support out of the box.
- JetBrains also provides a plugin for Eclipse.
- Integration with common Java build tools is supported including Apache Maven, Apache Ant, and Gradle.
- Android Studio (based on IntelliJ IDEA) has official support for Kotlin, starting from Android Studio 3.
- Emacs has a Kotlin Mode in its Melpa package repository.
Applications
One of the obvious applications of Kotlin is Android development. The platform was stuck on Java 7 for a while (with some contemporary language features made accessible through the use of Retrolambda or the Jack toolchain) and Kotlin introduces many improvements for programmers such as null-pointer safety, extension functions and infix notation. Accompanied by full Java compatibility and good IDE support (Android Studio) it is intended to improve code readability, give an easier way to extend Android SDK classes and speed up development.
Kotlin was announced as an official Android development language at Google I/O 2017. It became the third language fully supported for Android, in addition to Java and C++.
Adoption
According to the Kotlin website, Prezi is using Kotlin in the backend. DripStat has done a writeup of their experience with Kotlin.
According to Jetbrains blog, Kotlin is used by Amazon Web Services, Pinterest, Coursera, Netflix, Uber, Square, Trello, Basecamp, and others. Corda, a distributed ledger developed by a consortium of well-known banks (such as Goldman Sachs, Wells Fargo, J.P. Morgan, Deutsche Bank, UBS, HSBC, BNP Paribas, Société Générale), has over 90% Kotlin in its codebase.
According to Google, Kotlin has already been adopted by several major developers -- Expedia, Flipboard, Pinterest, Square, and others -- for their Android production apps.
See also
- Comparison of programming languages
- Kotlin Forum
References
- This article contains quotations from Kotlin tutorials which are released under a Apache 2.0 license.
External links
- Official website
- Web-Demo and examples
Source of the article : Wikipedia