Why Java Programmers Will Love Kotlin — Part 1

Reason 1: Write Less Code

Data Classes

// Java
public class Customer {
private Long id;
private String firstName;
private String lastName;
private String email;
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return this.id;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return this.firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return this.lastName;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return this.email;
}
}
// Kotlin
data class Customer(
val id: Long,
val firstName: String,
val lastName: String,
val email: String
)
// Kotlin
val customer = Customer(1L, "John", "Doe", "john.doe@domain.com")
val message = "Hello " + customer.firstName + " " + customer.lastName + "."
// Java
Customer cusomter = new Customer(1L, "John", "Doe", "john.doe@domain.com");
String message = "Hello " + customer.getFirstName() + " " + customer.getLastName() + ".";

Type Inference

// Kotlin
val name: String = “John Doe”
// Kotlin
val name = "John Doe" // String
val list = listOf(1, 2, 3, 4) // List<Int>
val names = listOf("Name1", "Name2", "Name3") // List<String>
// Java
String name = "John Doe";
List<Integer> list = Arrays.asList(1, 2, 3, 4);
List<String> names = Arrays.asList("Name1", "Name2", "Name3");
// Java
Map<String, List<Customer>> groupByLastName(List<Customer> customers) { … }
// Java
Map<String, List<Customer>> result = groupByLastName(customers);
// Kotlin
val result = groupByLastName(customers)

Named Function Parameters with Defaults

// Java
public int compute(int value) {
return compute(value, 10);
}
public int compute(int value, int divisor) { … }
// Kotlin
fun compute(value: Int, divisor: Int = 10): Int {
// compute result

}
// Kotlin
fun <T> Sequence<T>.joinToString(
separator: CharSequence = ", ",
prefix: CharSequence = "",
postfix: CharSequence = "",
limit: Int = -1,
truncated: CharSequence = "…",
transform: ((T) -> CharSequence)? = null
): String
// Kotlin
val list = listOf("A", "B", "C")
val str = list.joinToString() // produce "A, B, C"
// Kotlin
val str = list.joinToString("-") // produce “A-B-C”
// Kotlin
val str = list.joinToString(separater = "-", postfix = ".")
// produce “A-B-C.”

Smart Casts

// Java
int length(Object value) {
if (value instanceof String) {
return ((String) value).length();
} else if (value instanceof Integer) {
return (int) value;
} else {
return -1;
}
}
// Kotlin
fun length(value: Any): Int {
if (value is String) {
return value.length // we are sure value is String here
} else if (value is Int) {
return value // and value is Int here
} else {
return -1
}
}
// Kotlin
if (value is String && value.length > 10) { … }

No Checked Exceptions

String Templates

// Java
int age = 27;
String message = String.format("My age is %d.", age);
// Kotlin
val age = 27
val message = "My age is $age."
// Kotlin
val numberOfRows = 10_000
val totalTimeTakenMs = 2000L
val message = "Number of rows: $numberOfRows, total time: $totalTimeTakenMs ms, throughput: ${numberOfRows * 1000.0 / totalTimeTakenMs} rows/sec"

Summary

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store