Search code examples
androidkotlindisambiguation

How to tell the difference between Java and Kotlin code?


As a beginner to Android app development, I am finding code examples that do not identify whether they are written in Java or Kotlin. Even StackOverflow questions frequently omit the language tag.

Is there an easy "tell" in the code where you can immediately see which language is used? For example, I can distinguish C and C++ very quickly from certain elements of the syntax, header use, and library functions.

Are there any quick and "obvious" ways to distinguish Java from Kotlin? I want to be exploring Kotlin and not adding to my (immense) confusion by studying irrelevant code.


Solution

  • some obvious traits for kotlin:

    • as already mentioned the most obvious is semicolons, although you can add them and they will just be ignored

    • variables marked with question marks (nullability) val foo:String? and the usage of val/var/lateinit

    • class Foo : Something() <-- this is inheritance, there's no extends or implements

    • if files are involved, kotlin files end with .kt

    • any reference to companion object


    personally for me the biggest indicator:

    fun :) function declaration :

    fun foo(): String