Java To Kotlin PDF
Java To Kotlin PDF
Java To Kotlin PDF
Print to Console
Java
1 System.out.print("Amit Shekhar");
2 System.out.println("Amit Shekhar");
Kotlin
1 print("Amit Shekhar")
2 println("Amit Shekhar")
Concatenation of strings
Java
1 String firstName = "Amit";
2 String lastName = "Shekhar";
3 String message = "My name is: " + firstName + " " + lastName;
Kotlin
1 var firstName = "Amit"
2 var lastName = "Shekhar"
3 var message = "My name is: $firstName $lastName"
Ternary Operations
Java
1 String text = x > 5 ? "x > 5" : "x <= 5";
2
Bitwise Operators
Java
1 final int andResult = a & b;
2 final int orResult = a | b;
3 final int xorResult = a ^ b;
4 final int rightShift = a >> 2;
5 final int leftShift = a << 2;
6 final int unsignedRightShift = a >>> 2;
Kotlin
1 val andResult = a and b
2 val orResult = a or b
3 val xorResult = a xor b
4 val rightShift = a shr 2
5 val leftShift = a shl 2
6 val unsignedRightShift = a ushr 2
5 // if object is null
6 var car = object as? Car // var car = object as Car?
5 // if object is null
6 if (object is Car?) {
7 var car = object // smart casting, car will be null
8 }
Multiple conditions
Java
if (score >= 0 && score <= 300) { }
Kotlin
if (score in 0..300) { }
For-loops
Java
1 for (int i = 1; i <= 10 ; i++) { }
2
5 for (i in 10 downTo 0) { }
6
Collections
Java
1 final List<Integer> listOfNumber = Arrays.asList(1, 2, 3, 4);
2
8 // Java 9
9 final List<Integer> listOfNumber = List.of(1, 2, 3, 4);
10
for each
Java
1 // Java 7 and below
2 for (Car car : cars) {
3 System.out.println(car.speed);
4 }
5
6 // Java 8+
7 cars.forEach(car -> System.out.println(car.speed));
8
16 // Java 8+
17 cars.stream().filter(car -> car.speed > 100).forEach(car -> Syst
em.out.println(car.speed));
18 cars.parallelStream().filter(car -> car.speed > 100).forEach(car
-> System.out.println(car.speed));
Kotlin
1 cars.forEach {
2 println(it.speed)
3 }
4
8 // kotlin 1.1+
9 cars.stream().filter { it.speed > 100 }.forEach { println(it.spe
ed)}
10 cars.parallelStream().filter { it.speed > 100 }.forEach { printl
n(it.speed)}
Splitting arrays
java
1 String[] splits = "param=car".split("=");
2 String param = splits[0];
3 String value = splits[1];
kotlin
val (param, value) = "param=car".split("=")
Defining methods
Java
1 void doSomething() {
2 // logic here
3 }
Kotlin
1 fun doSomething() {
2 // logic here
3 }
6 // as a single-expression function
7
6 // as a single-expression function
7 fun getScore(value: Int): Int = 2 * value
8
Constructors
Java
1 public class Utils {
2
3 private Utils() {
4 // This utility class is not publicly instantiable
5 }
6
11 }
Kotlin
1 class Utils private constructor() {
2
3 companion object {
4
9 }
10 }
11
12 // another way
13
14 object Utils {
15
20 }
27 @Override
28 public boolean equals(Object o) {
29 if (this == o) return true;
30 if (o == null || getClass() != o.getClass()) return fals
e;
31
37 }
38
39 @Override
40 public int hashCode() {
41 int result = name != null ? name.hashCode() : 0;
42 result = 31 * result + age;
43 return result;
44 }
45
46 @Override
47 public String toString() {
48 return "Developer{" +
49 "name='" + name + '\'' +
50 ", age=" + age +
51 '}';
52 }
53 }
Kotlin
data class Developer(var name: String, var age: Int)
Cloning or copying
Java
1 public class Developer implements Cloneable {
2
11 @Override
12 protected Object clone() throws CloneNotSupportedException {
13 return (Developer)super.clone();
14 }
15 }
16
17 // cloning or copying
18 Developer dev = new Developer("Mindorks", 30);
19 try {
20 Developer dev2 = (Developer) dev.clone();
21 } catch (CloneNotSupportedException e) {
22 // handle exception
23 }
Kotlin
1 data class Developer(var name: String, var age: Int)
2
3 // cloning or copying
4 val dev = Developer("Mindorks", 30)
5 val dev2 = dev.copy()
6 // in case you only want to copy selected properties
7 val dev2 = dev.copy(age = 25)
Class methods
Java
1 public class Utils {
2
3 private Utils() {
4 // This utility class is not publicly instantiable
5 }
6
11 }
12
enum
Java
1 public enum Direction {
2 NORTH(1),
3 SOUTH(2),
4 WEST(3),
5 EAST(4);
6
7 int direction;
8
9 Direction(int direction) {
10 this.direction = direction;
11 }
12
Sorting List
Java
1 List<Profile> profiles = loadProfiles(context);
2 Collections.sort(profiles, new Comparator<Profile>() {
3 @Override
4 public int compare(Profile profile1, Profile profile2) {
5 if (profile1.getAge() > profile2.getAge()) return 1;
6 if (profile1.getAge() < profile2.getAge()) return -1;
7 return 0;
8 }
9 });
Kotlin
1 val profile = loadProfiles(context)
2 profile.sortedWith(Comparator({ profile1, profile2 ->
3 if (profile1.age > profile2.age) return@Comparator 1
4 if (profile1.age < profile2.age) return@Comparator -1
5 return@Comparator 0
6 }))
Anonymous Class
Java
1 AsyncTask<Void, Void, Profile> task = new AsyncTask<Void, Void,
Profile>() {
2 @Override
3 protected Profile doInBackground(Void... voids) {
4 // fetch profile from API or DB
5 return null;
6 }
7
8 @Override
9 protected void onPreExecute() {
10 super.onPreExecute();
11 // do something
12 }
13 };
Kotlin
1 val task = object : AsyncTask<Void, Void, Profile>() {
2 override fun doInBackground(vararg voids: Void): Profile? {
3 // fetch profile from API or DB
4 return null
5 }
6