DEV Community

Mobitsolutions
Mobitsolutions

Posted on

Java VS Kotlin : What Should i Choose for Android App Development?

Google introduced kotlin as second official language for android app development in 2017.Since that time, a rapid increase in demand of kotlin has been observed by the developers and enterprise community.

What Is Kotlin - An introduction?

A programming language that introduces functionality features to support Java interoperability. It has improved syntax than java and concise expressions and abstractions. Developers write lesser code as compared to java programming language.

What is the current status of Kotlin?

After the release of kotlin 1.3 improvements have been made to Kotlin/Native to accommodate the concept of multi-platform convenience. Just one IDE of android studio can be used to develop apps for multiple platforms with the default support of kotlin.

Many of the enterprise leaders are shifting their apps to kotlin,for example: Twitter, Netflix,Pinterest, Uber, Trello & AirBnB has shifted or planned to shift already.

Java’s weaknesses

Below are the 5 major weaknesses addressed by kotlin

1- No Checked Exceptions

Checked exceptions means when the compiler forces the caller of a function to catch or (again-throw) an exception. This fact is called, checked exceptions are unnecessary & cause empty catch blocks. When a checked exception does not exist its an annoying for developers because empty catch blocks force application developers to weed through the code to identify a non existing exception. Solution is here, Kotlin removes them completely, which helps to minimize verbosity & improves type-safety.

2- No Raw Types

Raw types were quite popular before generics came into play. because of their backward compatibility, but there is a problem with raw types as it throws a CastClassException & the error will show during execution time and, not the compiling time. Kotlin does not allow these raw types, & as a result, it generates a more type safe code.

3- Inbuilt Null Safety

Type system in kotlin has inbuilt null safety. The NullPointerException is big responsible for Android development mistakes. Android completely relies on a null to represent the absence of a value, but a null can destroy an app very easily. Kotlin has solution to this problem by incorporating inherent null-safety. This advancement saves developers from writing extra code to fix the issue.

4- Interoperability

Its the core purpose purpose of Kotlin. From start, Developers can simply code modules in Kotlin that work bug-free within existing Java language code. As Kotlin compiler allows the two languages to work by emitting the Bytecode in the same project.

5- Brevity

Kotlin is very precise, that's why developers love it. Lets have a comparison of code shortness


// in JAVA
static MobitFragment newInstance(String arg1, String arg2) {
MobitFragment Myfragment = new MobitFragment();
Bundle args = new Bundle();
args.putString(ARG_1_KEY, arg1);
args.putString(ARG_2_KEY, arg2);
fragment.setArguments(args);
return Myfragment ;
}

and the same code in Kotlin would look like


// In Kotlin
companion object {
fun newInstance(arg1: String, arg2: String): MobitFragment {
return MobitFragment().apply {
args = Bundle().apply {
putString(ARG_1_KEY, arg1)
putString(ARG_2_KEY, arg2)
}
}
}
}

What are the advantages of kotlin?

There are two major advantages seen in kotlin.

1- Fewer App Crashes

If you write lesser code as compared to java language, it will not only save development time but chances of crashed will be low. Identification of bugs would be easy as a result, less crashes will be reported.

2- Reduced Project Timelines

Kotlin is a short language than java, it gives an opportunity to developers to read and write code more efficiently and deliver project in timely manner.

Top comments (0)