August 2017
Intermediate to advanced
440 pages
10h
English
Another big improvement introduced by Kotlin is the way to access properties. In Java, we access a property using the corresponding method (setSpeed/getSpeed). Kotlin promotes property access syntax, which is a more expressive way of accessing properties. Let's compare both approaches, assuming we have a simple Car class that has a single speed property:
class Car (var speed: Double)
//Java access properties using method access syntax
Car car = new Car(7.4)
car.setSpeed(9.2)
Double speed = car.getSpeed();
//Kotlin access properties using property access syntax
val car: Car = Car(7.4)
car.speed = 9.2
val speed = car.speed
As we can see, in Kotlin there is no need to add get or set prefixes and ...
Read now
Unlock full access