0

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.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • 1
    The way variables are declared and the method signature. Variable declaration will start with the keywords 'var' or 'val', and method signatures are very different, but start with 'fun'. – eimmer Apr 28 '22 at 14:00

6 Answers6

2

If the code has used "val", "var" keywords then 99% it is Kotlin.

Yasiru Nayanajith
  • 1,647
  • 17
  • 20
  • 1
    Thank you, I know the difference in the usage between `val` and `var`, but have avoided studying Java at the same time. – Weather Vane Apr 28 '22 at 14:03
  • 1
    Not entirely true. Android now supports Java 11, which has the `var` keyword for declaring variables. `var foo = 1;` is valid in both Java and Kotlin. – Tenfour04 Apr 28 '22 at 15:11
2

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
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
2

Kotlin Keywords and operators. https://kotlinlang.org/docs/keyword-reference.html

Java Language Keywords. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html

Most likely kotlin:

val
var
fun
nameX: TypeX
nameX : TypeX?
object: 
open class
when
@
:
sealed
!!
->
"$name or ${name}"
etc

Most likely java:

; semicolon
TypeX nameX
void
extends 
implements
public class
instanceof
etc
ocos
  • 1,901
  • 1
  • 10
  • 12
1

Java has mandatory semicolons ';' while Kotlin doesn't. The types in Java are int, string, char, etc while in Kotlin they are capitalized(String, Int, Long, Char), also Kotlin can infer the type of a variable most of the time ;)

MercifulSory
  • 337
  • 1
  • 14
  • 1
    Thanks, you can see the stage I am at! I'd got the type inference but the semicolon really is obvious. I knew they were *optional* in Kotlin but not that Java is mandatory. – Weather Vane Apr 28 '22 at 14:00
1

i found this article helpful
as guys told kotlin is a language Is introduced by null safety and without any smicolons force and you can find some key in syntax like

**[fun,as,let,also,apply,val,var,lateinit,...]**

So, which one is better and should you use it? The answer to that question depends on your needs. If you’re looking for a language with solid support from Google, then Kotlin may be the best choice, as Android Studio 3 now supports Kotlin development. However, if you need speed or want an open-source project with more flexibility (especially in terms of third-party libraries), Java might be the right option for you.

refer to this article

amir.ashrafi
  • 107
  • 2
  • 11
  • Thanks, and that touches on another matter. I am staggered by the sheer code bloat from even the simplest of activities. The "Hello, World!" source folder is an astonishing **20 Mbytes**! I can't see the need for 3rd-party libraries yet even some quite basic acitivities are using them, and Android Developer Studio is asking me if I trust them. I have to reject what might be useful material. – Weather Vane Apr 28 '22 at 14:12
-1

If the file end with .kt, it is Kotlin.

Ethan
  • 1
  • 1