Kotlin vs Java

Recently, you might have come across a word Kotlin, so in this post, I am going to share about Kotlin and how it is different from JAVA?

What is Kotlin?

Kotlin is statically typed programming language developed by JetBrains that runs on Java Virtual Machine. It can also be compiled to JavaScript source code. Kotlin is expressive, concise and powerful as it combines object-oriented Programming and functional programming features. It is focused on interoperability, safety and tooling support. Lately, it is really popular within the Android community and most of the developers are switching towards Kotlin. The reason being that Kotlin reduces the lines of code and make it more convenient process for the developers.


Let's look at the few important differences between Kotlin and Java


Kotlin vs Java :


  • Function Types: Kotlin has the ability to pass the function as a parameter to other functions without resorting to single abstract methods like callable, runnable etc, unlike Java
  •  Null Safety: Kotlin has no danger of getting NullPointerException unlike Java, the reason being that Kotlin is Null safe as all the types in Kotlin are non-nullable (it could not hold null value) but it can be modified to hold null values by adding ? at the end of the variables. Kotlin executes forced checks while compiling and prevents the usage of null variables 
  • Checked Exceptions: Kotlin doesn't have any checked exceptions, so you don't need to declare or catch any exceptions unlike Java. But the efficiency and inefficiency of checked exceptions is still the matter of personal opinion among the developers.
  • Type Casting: In Java often you need to check the type and cast an object, but in Kotlin you don't need to explicitly cast the types as it has a feature of smart type casting which automatically detects the type if you already checked it with Kotlin's 'is' operator.
         if (object is String)
            print(object.length)   // object is automatically casted to String



  • Array Invariance: Arrays in Java are covariant whereas arrays in Kotlin are invariant. For example in Java above code assigns the Number[] value to Object[] variable because of Java array covariance and it will not show any error during compilation but during execution, it will show error.
          Number[] num = new Number[1];
          Object[] objects = num;
          objects[0] = "abc";


  • Support for Constructors: Unlike Java, a Kotlin class can have primary constructors and one or more secondary constructors. The primary constructor is a part of class header.
          class Employee constructor(firstName: String){...}


         The class can also declare secondary constructor which is prefixed with constructor.
     
         class Employee {
            constructor(parent: Employee) {
                parent.children.add(this)
               } 
               }

          
Also, Check out below blog post in JAVA

Tricky Questions in JAVA

Concurrent Hash Map in JAVA


Comments

Popular posts from this blog

Tricky Questions or Puzzles in C

Program to uncompress a string ie a2b3c4 to aabbbcccc

Number series arrangement puzzles