Why is Kotlin better than Java?

This is a response to the translated post Why is Kotlin Worse Than Java? ... Since the original argumentation is based on only two examples, we will not waste time going through these "shortcomings" of Kotlin.





Proprietary metadata?

a fair amount of details about the inner workings of kotlinc are hidden inside the generated class files ... without IDEA, Kotlin would die immediately





This is not proprietary code, but just a way for the compiler to add additional data in a hard-coded .class file format, which was previously only sharpened for javac. Metadata is needed for reflection and can be removed during compilation. The source code of the metadata is open source and publicly available .





Kotlin will lag behind?

In short, the message of the original article is that Kotlin was innovative, but Java will add all the same language features, only more thoughtful and better, and already the Kotlin version will fall out of the mainstream.

As an example, the author gives instanceof



:





Kotlin did something like this





if (x instanceof String) { 
  //  x   String!   
  System.out.println(x.toLowerCase()); 
}
      
      



But in Java version 16+ it became like this:





if (x instanceof String y) {  
  System.out.println(y.toLowerCase()); 
}
      
      



, , . , ยซยป, Kotlin -, Kotlin , Java. , Java : , ยซ ยป (, ยซยป -).

...

Java . instanceof , , Kotlin Java: , Java - ( , JEP ) , Kotlin.





Kotlin. , Kotlin . , ( ), Kotlin , , :





when(val v = calcValue()) {
  is String -> processString(v)
}
      
      



, . Java c if/instanceof/switch:





when(val v = calcValue()) {
  is String -> processString(v)
  42 -> prosess42()
  is Int -> processInt(v)
  else -> processElse(v)
}
      
      



, Kotlin, , - Java, , , , Kotlin.





Kotlin , , Java, .





, Kotlin . , Java. C# Kotlin Java , Java , , . Java , , .





null, , Java Optional.ofNullable



. Data- record



.





Java , , . Optional null, record data class.





, Kotlin ?





, Scala Java. , ++ .

, Java . Kotlin, , Android , backend jvm . Kotlin , , , .








All Articles