September 2023
Intermediate to advanced
470 pages
11h 12m
English
Let’s see what Kotlin looks like, comparing the basic syntax constructs with their Java equivalents.
In Java, to declare a temporary variable, you have to write the type before the name of the variable. Starting with Java 10, it’s also possible to use the keyword var, and let the compiler figure out the exact type. For example:
| | int a = 4; |
| | var b = 5; |
| | b = a + b; |
In Kotlin, the type is always optional if it can be inferred, and is declared after the variable name, but there are two variable keywords: val for immutable (that is, not reassignable) variables and var for variables that can be reassigned, like this:
| | val a: Int = 4; |
| | var b = 5; |
| | b = a + b; |
As a rule of thumb, it’s better to avoid using the
Read now
Unlock full access