From Java To Kotlin PDF
From Java To Kotlin PDF
BASICS
Java Kotlin
System.out.print("Hello, World!"); print("Hello, World!")
System.out.println("Hello, World!"); println("Hello, World!")
Variables I
Java Kotlin
final int x; val x: Int
final int y = 1; val y = 1
Variables II
https://fabiomsr.github.io/from-java-to-kotlin/ 1/9
9/17/2019 From Java to Kotlin
Java Kotlin
int w; var w: Int
int z = 2; var z = 2
z = 3; z = 3
w = 1; w = 1
Null I
Java Kotlin
final String name = null; val name: String? = null
Null II
Java Kotlin
if(text != null){ val length = text?.length
int length = text.length();
} val length = text!!.length // NullPointerException if text == null
Strings I
https://fabiomsr.github.io/from-java-to-kotlin/ 2/9
9/17/2019 From Java to Kotlin
Java Kotlin
String name = "John"; val name = "John"
String lastName = "Smith"; val lastName = "Smith"
String text = "My name is: " + name + " " + lastName; val text = "My name is: $name $lastName"
String otherText = "My name is: " + name.substring(2); val otherText = "My name is: ${name.substring(2)}"
Strings II
Java Kotlin
String text = "First Line\n" + val text = """
"Second Line\n" + |First Line
"Third Line"; |Second Line
|Third Line
""".trimMargin()
Ternary Operator
Java Kotlin
String text = x > 5 ? "x > 5" : "x <= 5"; val text = if (x > 5)
"x > 5"
else "x <= 5"
https://fabiomsr.github.io/from-java-to-kotlin/ 3/9
9/17/2019 From Java to Kotlin
BASICS
Bits Operations
Java Kotlin
final int andResult = a & b; val andResult = a and b
final int orResult = a | b; val orResult = a or b
final int xorResult = a ^ b; val xorResult = a xor b
final int rightShift = a >> 2; val rightShift = a shr 2
final int leftShift = a << 2; val leftShift = a shl 2
Is As In
Java Kotlin
if(x instanceof Integer){ } if (x is Int) { }
Smart Cast
https://fabiomsr.github.io/from-java-to-kotlin/ 4/9
9/17/2019 From Java to Kotlin
Java Kotlin
if(a instanceof String){ if (a is String) {
final String result = ((String) a).substring(1); val result = a.substring(1)
} }
Switch / When
Java Kotlin
final int x = // value; val x = // value
final String xResult; val xResult = when (x) {
0, 11 -> "0 or 11"
switch (x){ in 1..10 -> "from 1 to 10"
case 0: !in 12..14 -> "not from 12 to 14"
case 11: else -> if (isOdd(x)) { "is odd" } else { "otherwise" }
xResult = "0 or 11"; }
break;
case 1: val y = // value
case 2: val yResult = when {
//... isNegative(y) -> "is Negative"
case 10: isZero(y) -> "is Zero"
xResult = "from 1 to 10"; isOdd(y) -> "is odd"
break; else -> "otherwise"
default: }
if(x < 12 && x > 14) {
xResult = "not from 12 to 14";
break;
}
if(isOdd(x)) {
xResult = "is odd";
break;
}
xResult = "otherwise";
}
https://fabiomsr.github.io/from-java-to-kotlin/ 5/9
9/17/2019 From Java to Kotlin
if(isNegative(y)){
yResult = "is Negative";
} else if(isZero(y)){
yResult = "is Zero";
}else if(isOdd(y)){
yResult = "is Odd";
}else {
yResult = "otherwise";
}
For
Java Kotlin
for (int i = 1; i < 11 ; i++) { } for (i in 1 until 11) { }
Collections
Java Kotlin
final List<Integer> numbers = Arrays.asList(1, 2, 3); val numbers = listOf(1, 2, 3)
https://fabiomsr.github.io/from-java-to-kotlin/ 6/9
9/17/2019 From Java to Kotlin
final Map<Integer, String> map = new HashMap<Integer, String>(); val map = mapOf(1 to "One",
map.put(1, "One"); 2 to "Two",
map.put(2, "Two"); 3 to "Three")
map.put(3, "Three");
// Java 9
final List<Integer> numbers = List.of(1, 2, 3);
Collections
Java Kotlin
for (int number : numbers) { numbers.forEach {
System.out.println(number); println(it)
} }
Collections
Java Kotlin
final Map<String, List<Integer>> groups = new HashMap<>(); val groups = numbers.groupBy {
for (int number : numbers) { if (it and 1 == 0) "even" else "odd"
https://fabiomsr.github.io/from-java-to-kotlin/ 7/9
9/17/2019 From Java to Kotlin
if((number & 1) == 0){ }
if(!groups.containsKey("even")){
groups.put("even", new ArrayList<>());
}
groups.get("even").add(number);
continue;
}
if(!groups.containsKey("odd")){
groups.put("odd", new ArrayList<>());
}
groups.get("odd").add(number);
}
// or
Collections
Java Kotlin
final List<Integer> evens = new ArrayList<>(); val (evens, odds) = numbers.partition { it and 1 == 0 }
final List<Integer> odds = new ArrayList<>();
for (int number : numbers){
if ((number & 1) == 0) {
evens.add(number);
}else {
odds.add(number);
}
}
https://fabiomsr.github.io/from-java-to-kotlin/ 8/9
9/17/2019 From Java to Kotlin
Collections
Java Kotlin
final List<User> users = getUsers(); val users = getUsers()
users.sortedBy { it.lastname }
Collections.sort(users, new Comparator<User>(){
public int compare(User user, User otherUser){
return user.lastname.compareTo(otherUser.lastname);
}
});
// or
https://fabiomsr.github.io/from-java-to-kotlin/ 9/9