0% found this document useful (0 votes)
191 views

From Java To Kotlin PDF

This document compares basic programming concepts between Java and Kotlin, including: - Printing and declaring variables differ slightly between the languages. Kotlin uses more concise syntax than Java. - Kotlin handles null values differently than Java by making null-safety a priority. Variables can be declared to allow or disallow null values. - String manipulation in Kotlin is simpler with string templates that allow variable interpolation directly in strings. - Control structures like if/else, when/switch, and for loops have improved syntax in Kotlin that make code more readable compared to Java.

Uploaded by

Mridupaban Dutta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
191 views

From Java To Kotlin PDF

This document compares basic programming concepts between Java and Kotlin, including: - Printing and declaring variables differ slightly between the languages. Kotlin uses more concise syntax than Java. - Kotlin handles null values differently than Java by making null-safety a priority. Variables can be declared to allow or disallow null values. - String manipulation in Kotlin is simpler with string templates that allow variable interpolation directly in strings. - Control structures like if/else, when/switch, and for loops have improved syntax in Kotlin that make code more readable compared to Java.

Uploaded by

Mridupaban Dutta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

9/17/2019 From Java to Kotlin

From Java to Kotlin

Basic Functions Classes

BASICS

Print

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

String lastName; var lastName: String?


lastName = null lastName = null

var firstName: String


firstName = null // Compilation error!!

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) { }

final String text = (String) other; val text = other as String

if(x >= 0 && x <= 10 ){} if (x in 0..10) { }

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

final int y = // value;


final String yResult;

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) { }

for (int i = 1; i < 11 ; i+=2) { } for (i in 1..10 step 2) {}

for (String item : collection) { } for (item in collection) {}


for ((index, item) in collection.withIndex()) {}
for (Map.Entry<String, String> entry: map.entrySet()) { }
for ((key, value) in map) {}

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);

final Map<Integer, String> map = Map.of(1, "One",


2, "Two",
3, "Three");

Collections

Java Kotlin
for (int number : numbers) { numbers.forEach {
System.out.println(number); println(it)
} }

for (int number : numbers) { numbers.filter { it > 5 }


if(number > 5) { .forEach { println(it) }
System.out.println(number);
}
}

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

Map<String, List<Integer>> groups = items.stream().collect(


Collectors.groupingBy(item -> (item & 1) == 0 ? "even" : "odd")
);

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

users.sort(Comparator.comparing(user -> user.lastname));

https://fabiomsr.github.io/from-java-to-kotlin/ 9/9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy