Hi, Today we will be talking about some of the differences any Java developer will notice while trying to learn Kotlin.

  • You don’t have to put ‘;‘ at the end of the statement, but whenever you want you can use it, the compiler will just ignore it.
  • The wrapper in Kotlin, like when we used println instead of System.out.println.In fact, Kotlin is still calling System.out.println but it is under the cover.
  • Keywords, here we can check that Kotlin has both hard keywords and soft keywords. Hard keywords are like java you cant use them as identifiers, but soft keywords act as keywords in the context when they are applicable and can be used as identifiers in other contexts.
  • we can use square brackets to access items in a collection instead of using names.get in the following example we will do it like this:
    val names = arrayListOf("besho","ali","antonio") 
    println(names[1])
  • Kotlin uses its own String class which hides some methods in the original Java String class,  for example in java to get a length of string we used to call Str2.length() but in Kotlin it is just a property we don’t need to call a method for it.  The information about the Kotlin string class can be found here.
  • In Kotlin, there are no checked exceptions, no exceptions have to be declared and you aren’t forced to catch any exception, though, of course, you can. Even when deriving from a Java class, you don’t have to declare exceptions that a method throws.
  • In kotlin there is no ternary operator a ? b : c instead, In Kotlin if statements are expressions. So the following code is equivalent: if (a) b else c .
  • For loops also changed in Kotlin, we cant do this anymore: for (int i=0;i<100;i++){ } ,
    Instead, Kolin’s for loop is equivalent to the foreach loop in languages like C#. The syntax is as follows:
    for (item in collection) print(item)
  • We don’t have the static keyword, and we dont have to use the keyword new while creating objects.

These are some of the main differences I found till now, I will be updating this post when I notice a new one.

See You Later 😉