Java Vs Kotlin - Different between Java-Android and Kotlin-Why we switch to kotlin

 
LPK Technosoft

When you think about Android development, chances are one programming language immediately springs to mind: Java.

While it’s true that the majority of Android apps are written in Java, when it comes to Android development, Java isn’t your only option.

You can write Android apps in any language that can compile and run on the Java Virtual Machine (JVM), and your end users will be none the wiser. And one JVM-compatible programming language that’s really caught the attention of the Android community is Kotlin, a statically typed programming language from JetBrains.

Why Should I Make the Switch From Java?

The biggest issue is that Java isn’t a modern language and although Java 8 was a huge step forward for the platform, introducing lots of features that developers had been waiting for (including lambda functions), at the time of writing Android only supports a subset of Java 8 features. It seems unlikely that Android developers will be reaping the full benefits of Java 8 anytime soon, so for the foreseeable future if you want to use Java in your Android projects, then you’re pretty much stuck with Java 7.

 

Java as a whole also has some pretty well-documented language issues, including endless try-catch blocks, a lack of extendability, null-unsafety (and that infamous NullPointerException), not to mention a lack of support for functional programming

features. Although Java is starting to add some functional programming elements, such as lambda expressions and functional interfaces, at its core Java is still a procedural language. Java’s syntax is also pretty verbose, particularly when compared to many modern programming languages.


Kotlin

Kotlin came when Android development needed a more modern language to add to the qualities of Java and aid in mobile development.  JetBrains, the people who created IntelliJ, the integrated development environment also created Kotlin.  It is an open-source, statically typed language based on Java Virtual Machine (JVM). The advantage with Kotlin is that you can compile it to JavaScript and interoperate it with Java. This allows developers to not only easily update old Java apps to Kotlin, but also carry on their old work in Java to Kotlin. try kotlin now

Pros of Kotlin

Kotlin is definitely faster to write and thus, much loved by developers. If it takes 50 lines of code in Java, it only takes 1-2 lines in Kotlin. This also means there are much fewer errors and bugs.

Kotlin aids in the building of clean APIs

Kotlin is null-safe by default

NullPointerExceptions are a huge source of frustration for Java developers. Java allows you to assign null to any variable, but if you try to use an object reference that has a null value, then brace yourself to encounter a NullPointerException!

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 Swift.

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

fun MutableList<Int>.swap(index1: Int, index2: Int) {

    val tmp = this[index1] // 'this' corresponds to the list

this[index1] = this[index2]

    this[index2]= tmp

}

You can then call this function on instances of the extended class, via the. notation, as if it were part of that class:

val l = mutableListOf(1, 2, 3)
l.swap(0, 2) // 'this' inside 'swap()' will hold the value of 'l'

Above examples, there are many things that we cannot do in Java but in kotlin, we can

Java vs Kotlin Programming Example

Java Code

MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
    private Button sayHello;
    private TextView hello;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        hello = (TextView)findViewById(R.id.helloText);
        sayHello = (Button)findViewById(R.id.helloButton);
        sayHello.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hello.setText("Hello Java. I am currently running on JVM");
                hello.setVisibility(EditText.VISIBLE);
            }
        });
    }
}


Kotlin Programming

So our MainActivity.java file can be written in MainActivity.kt as bellow

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
class MainActivity : AppCompatActivity() {
    private var sayHello: Button? = null
    private var hello: TextView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        hello = findViewById(R.id.helloText) as TextView?
        sayHello = findViewById(R.id.helloButton) as Button?
        sayHello!!.setOnClickListener {
            hello!!.text = "Hello Kotlin. I am currently running on JVM"
            hello!!.visibility = EditText.VISIBLE
        }
    }
}
import android.os.Bundle;  // the usual way of indicating a statement
import android.os.Bundle       // the Kotlin way without ;

In Java, it's a doctrine to have the class name that has the same name as a file name to be a public class. It's not necessary for kotlin if you are inheriting a class. It assumes the same visibility modifier for a child class

// java way of declaring a public class
public class MainActivity extends AppCompatActivity {
}

the kotlin equivalent without "public" - visibility modifier

// the kotlin way of class declaration
class MainActivity : AppCompatActivity() {
}

Conclusions, Tips, and advice

Kotlin brought simplicity to android development. We have seen these in their approach to inheritance, casting keyword, redundant semi-colon which most recent languages like javascript, swift among others also consider not necessary.

I think Kotlin is easier and simple to learn than Java but the current disadvantages are that there, not much documentation and online learning resources yet as you will have thousands of learning resource all over the internet for Java. However, the good part is that people are here for you to take up your challenges in building an android app with kotlin or learning kotlin.

Another good side of it is that they are a good way to get good StackOverflow reputations both for the questions asked and the solution proffered, codementor's ratings and reviews from mentee and a good way to start a career in kotlin. So in all, I think it won't be bad to try out new things with kotlin for android beginner, Intermediate and or expert developers.

 




Comments

No Comment

Leave a Comment

Your email address will not be published.



First Floor, Madhav Complex, V. R. Nagar, Mirjapar Madhapar By Pass Ring Road, Bhuj-370001
+(91) 97 26 134340
Mon-Fri 9:00am-6:00pm
talk@lpktechnosoft.com
24 X 7 online support