What are the advantages of Kotlin over Java for developing for Android for a programmer?

I am superficially familiar with Java, read only a brief description of Kotlin, so far I only see:
- more compact syntax
- there are properties
- there seems to be references to functions (which theoretically could greatly simplify the task of any handlers for pressing buttons, etc.)
- you can declare functions without classes

Somewhere else I saw that Kotlin has nullable and non-nullable variables, it’s just not clear what it gives and why it is so important.

I ask a question because I decide - in what language to start a new project for Android - not a very big application for the data collection terminal.

  • one
    well, and bonus coroutines - iFr0z
  • Well, there is still some good sugar - Anton Sorokin

2 answers 2

Choose Kotlin. With him much better. After him, Java will seem wordy and dangerous.

The advantages of the move can call such:

  1. data classes with default argument values. No longer need huge model classes. Just describe the constructor and everything - toString, getter / setter, equals / hashCode will be generated automatically and invisibly.
  2. Operations with collections. To convert a sheet of one type to another, you do not need to write a cycle. It will be written for you in bytecode. val intList = stringList.map { it.toInt() }
  3. Expansion Functions Extremely comfortable. Replace Utilities classes with static methods and look extremely nice: listOfObjectWithStringAndInt.sortBy { it.intValue }
  4. For specifically Android, you can use kotlin-android-extensions - in the classes that work with markup you do the import and you will have access to the generated variables for all views in the markup.
  5. Nullability is incredibly handy. If you need to call a method on a variable that may be null , then simply write nullableVar?.methodName() - the method will be called only if the variable is not null . If you declare a variable as не-nullable , the compiler will not allow you to pass a nullable variable there. This solves many problems.
  • four
    All of the above has its price. And if for simple applications it “will do so”, then for more complex things one should at least roughly imagine what will be compiled in comparison with the same java. I myself love Kotlin very much, but sometimes I get thoughts that it will not cause a wave of “not thinking about the consequences” of developers. :) - rjhdby
  • one
    @rjhdby, I don’t argue) I’ve come across situations where only an understanding of what the curly brackets turn into when broadcasting to Java helped to understand why the code doesn’t work)

This is my personal, estimated, opinion. It does not claim to be the ultimate truth, and other people may quite legitimately have the opposite opinion.

more compact syntax

Not "compact", but ideological. The development process for the most part implies not writing code, but reading what has already been written. The simpler the code is, the more productive the development process.

If in a very rough approximation, Kotlin can be described as Java with a huge amount of syntactic sugar, which allows you to do the same by writing much less, but the bytecode will be the same.

there are properties

there seems to be links to functions

you can declare functions without classes

See point 1.

Somewhere else I saw that Kotlin has nullable and non-nullable variables, it’s just not clear what it gives and why it is so important.

Nullability is an attempt to make the type system more strict. There is a direct relationship: the stricter the type system, the fewer errors will be further compiled. An NPE is a “billion-dollar error” (c) - the pain and hatred of languages ​​where it is. Any tightening that allows you to avoid it is a blessing.

I ask a question because I decide - in what language to start a new project for Android - not a very big application for the data collection terminal.

It all depends on who you see yourself in the future. If only Android developer, then Kotlin is your choice. If in principle developers under JVM (in a bloody enterprise for example), then Java. But if on a large scale, then Java is a must have in any case, if only because without knowing it you will not understand what is happening in Kotlin.

  • But on Kotlin you can write on the back. There are several companies (like Tinkoff) who do this. There are also reports on this topic. - Anton Sorokin