Kotlin

Kotlin

Kotlin is an open source, statically typed language based on Java Virtual Machine (JVM), with 100% interoperable with Java.

Note:- You can also compile it to JavaScript or Native for building code that can run on iOS.

Interoperable with Java, i.e. You can have Java and Kotlin code existing side by side in the same project.

Statically typed language

A language is statically typed if the type of a variable is known at compile time. i.e. it does type checking at compile-time.

For some languages, this means that you as the programmer must specify what type each variable is (e.g.: Java, C, C++), Other languages offer some form of type inference, the capability of the type system to deduce the type of a variable (e.g.: OCaml, Haskell, Scala, Kotlin)

The main advantage here is that all kinds of checking can be done by the compiler, and therefore a lot of trivial bugs are caught at a very early stage.

Dynamically Typed languages

Dynamically typed programming languages do type checking at run-time as opposed to Compile-time.

A language is dynamically typed if the type is associated with run-time values, and not named variables/fields/etc. This means that you as a programmer can write a little quicker because you do not have to specify types every time. Example: Perl, Ruby, Python.

Note: both Java and kotlin are statically typed language

Kolin vs Java

1) Kotlin is null-safe by default
Java allows one to assign null to any variable, but if one uses that object reference, one will get NullPointerException.

In Kotlin, all types are non-nullable (unable to hold a null value) by default.

If you try to assign or return null in your Kotlin code, then it’ll fail at compile-time.

If you really want to assign a null value to a variable in Kotlin, then you’ll need to explicitly mark that variable as nullable, by adding a question mark after the type:

 

2) Kotlin has more succinct/concise code as compare to Java

Kotlin reduces the boilerplate codes.

Example:- In Android findViewByIds.

Kotlin

Java

 

3) Extension functions
Kotlin gives developers the ability to extend a class with new functionality, which is ideal if there’s a class that you always felt was missing an important method!

These ‘extension functions’ aren’t available in Java, although they are available in other programming languages that you can use for Android development, such as C#.

You create an extension function by prefixing the name of the class you want to extend (such as ‘String’) to the name of the function you’re creating (‘styleString’) for example:

 

4) Coroutines

java does have it.
Coroutines are computer-program components that generalize subroutines for non-preemptive multitasking, by allowing multiple entry points for suspending and resuming execution at certain locations.

In Simpler term
Coroutines are general control structures where flow control is cooperatively passed between two different routines without returning.

Read More

Note:

  • In Java we can use Threads for this type of work.
  • Coroutines are also stackless, so they have a lower memory usage compared to threads.

 

5) There are no checked exceptions
Kotlin does not have checked exceptions, so you don’t need to catch or declare any exceptions.

6) Data classes

In java, even for small data class we have a lots of boilerplate code(constructors, setter, getter etc). In Kotlin, if you include the ‘data’ keyword in your class definition, then the compiler will perform all of this work for you, including generating all the necessary getters and setters:

Must read this

Conclusion:-  kotlin tries to simplify boilerplate java codes to a simpler one.

Posted in Android.