It has been several years since Kotlin came out, and it has been doing well. Since it was created specifically to replace Java, Kotlin has naturally been compared with Java in many respects.
To help you decide which of the two languages you should pick up, I will compare some of the main features of each language so you can choose the one you want to learn.
These are the 8 points I’ll discuss in this article:
To start it all let’s do some basic syntax comparison. Many of you who are reading this might already have some knowledge about Java and/or Kotlin, but I will give out a basic example below so we can compare them directly:
public class HelloClass {
public void FullName(String firstName, String lastName) {
String fullName = firstName + " " + lastName;
System.out.println("My name is : " + fullName);
}
public void Age() {
int age = 21;
System.out.println("My age is : " + age);
}
public static void main(String args[]) {
HelloClass hello = new HelloClass();
hello.FullName("John","Doe");
hello.Age();
}
}
class NameClass {
fun FullName(firstName: String, lastName:String) {
var fullName = "$firstName $lastName"
println("My Name is : $fullName")
}
}
fun Age() {
var age : Int
age = 21
println("My age is: $age")
}
fun main(args: Array<String>) {
NameClass().FullName("John","Doe")
Age()
}
The feel of the code isn’t that different aside from some small syntax changes in the methods and classes.
But the real difference here is that Kotlin supports type inference where the variable type does not need to be declared. Also we don’t need semicolons ( ;
) anymore.
We can also note that Kotlin does not strictly enforce OOP like Java where everything must be contained inside a class. Take a look at fun Age
and fun main
in the example where it is not contained inside any class.
Kotlin also typically has fewer lines of codes, whereas Java adheres more to a traditional approach of making everything verbose.
One advantage of Kotlin over Java is Kotlin’s flexibility – it can choose to do everything in the traditional OOP approach or it can go a different way.
#kotlin #java #developer