Java
 
Kotlin
 
Comments
Root of Class Hierarchy
 
java.lang.Object
 
kotlin.Any
 
Include methods/functions hashCode, equals, toString
Returning Nothing
 
Keyword "void" or java.lang.Void
 
kotlin.Unit
 
 
Something Never Exists or No Instances
 
No Equivalence
 
kotlin.Nothing
 
 
Entry Point
 
public static void main(Sting[] args) {}
 
fun main(args : Array<String>) {}
 
 
Class Declaration
 
public class MyClass {}
 
class MyClass {}
 
 
Data Class Declaration
 
public class MyDataClass {}
 
data class MyDataClass {}
 
In Java, data class can be considered as an entity class (annotated with @javax.persistence.Entity) or simply a POJO/JavaBean.
In Kotlin, the functions hashCode, equals, and toString are overridden such that properties between 2 objects are compared in addition to object references.
Byte Declaration & Assignment
 
byte x = 2;
Byte x = 2;
 
val x : Byte = 2
 
 
Short Declaration & Assignment
 
short x = 2;
Short x = 2;
 
val x : Short = 2
 
 
Integer Declaration & Assignment
 
int x = 2;
Integer x = 2;
 
val x : Int = 2
 
 
Long Declaration & Assignment
 
long x = 2L;
Long x = 2L;
 
val x : Long = 2L
 
 
Float Declaration & Assignment
 
float x = 2.0F;
Float x = 2.0F;
 
val x : Float = 2.0F
 
 
Double Declaration & Assignment
 
double x = 2.0;
Double x = 2.0;
 
val x : Double = 2.0
 
 
Boolean Declaration & Assignment
 
boolean x = true;
Boolean x = true;
 
val x : Boolean = true
 
 
Character Declaration & Assignment
 
char x = 'a';
Character x = 'a';
 
val x : Char = 'a'
 
 
String Declaration & Assignment
 
String x = "a";
 
val x : String = "a"
 
 
Array Declaration & Assignment
 
int[] x = new int[] {1, 2, 3};
 
val x : Array<Int> = arrayOf(1, 2, 3)
 
 
Collection Declaration & Assignment
 
List<T> myList = new ArrayList();
Set<T> mySet = new HashMap();
Map<K,V> myMap = new HashMap();
 
val myImmutableList : List<T> = listOf()
val myMutableList : List<T> = mutableListOf()
val myImmutableSet : Set<T> = setOf()
val myMutableSet : Set<T> = mutableSetOf()
val myImmutableMap : Map<K,V> = mapOf()
val myMutableMap : Map<K,V> = mutableMapOf()
 
T = type, K = key, V = value.
Java list, set, and map are mutable.
Variable Arguments
 
public void myMethod(int i, String ... args) {}
 
fun myFunction(i : Int, varargs args : String) {}
 
Applicable to last parameter only
Nullable Declaration & Assignment
 
Integer x = 2;
x = null;
 
var x : Int? = 2
x = null
 
 
Addition
 
int x = a + b;
 
val x : Int = a + b
 
In Java + is also for string concatenation
Addition Then Assignment
 
int x += a;
 
val x : Int += a
 
 
String Concatenation
 
int count;
System.out.println("Current count value is " + count);
 
Not needed because using $variable instead, example:
var count : Int
println("Current count value is $count")
 
 
Subtraction
 
int x = a - b;
 
val x : Int = a - b
 
 
Subtraction Then Assignment
 
int x -= a;
 
val x : Int -= a
 
 
Multiplication
 
int x = a * b;
 
val x : Int = a * b
 
 
Multiplication Then Assignment
 
int x *= a;
 
val x : Int *= a
 
 
Division
 
int x = a / b;
 
val x : Int = a / b
 
 
Division Then Assignment
 
int x /= a;
 
val x : Int /= a
 
 
Remainder (Modulus)
 
int x = a % b;
 
val x : Int = a % b
 
 
Post Increment
 
int x = a++;
 
val x : Int = a++
 
 
Pre Increment
 
int x = ++a;
 
val x : Int = ++a
 
 
Post Decrement
 
int x = a--;
 
val x : Int = a--
 
 
Pre Decrement
 
int x = --a;
 
val x : Int = --a
 
 
Logical (Short-Circuit) AND
 
a && b
 
a && b
 
 
Logical (Short-Circuit) OR
 
a || b
 
a || b
 
 
Logical NOT
 
!a
 
!a
 
 
Equals To
 
a == b
 
a == b
 
Value comparison.
== implicitly calls equals.
By default, method/function equals checks whether 2 objects are the same underlying objects (pointing to the same reference), aka object equivalence.
For data class, == is overridden to check also the properties.
Triple Equals (===)
 
No Equivalence
 
a === b
 
Value and type comparison.
Does NOT rely on function equals. Checks both the properties as well as references between 2 objects, aka object identity.
Not Equals To
 
a != b
 
a != b
 
 
Less Than
 
a < b
 
a < b
 
 
Less Than or Equals To
 
a <= b
 
a <= b
 
 
Greater Than
 
a > b
 
a > b
 
 
Greater Than or Equals To
 
a >= b
 
a >= b
 
 
Console Output Print
 
System.out.print("abc");
 
print("abc")
 
 
Console Output Print Line
 
System.out.println("abc");
 
println("abc")
 
 
If/Else
 
if (a == b) {
//statements
} else {
//statements
}
 
if (a == b) {
//statements
} else {
//statements
}
 
 
Ternary Operation
 
if (booleanExpression) ? expression1 : expression2;
 
No Equivalence
 
 
While Loop
 
while (a == b) {
//statements
}
 
while (a == b) {
//statements
}
 
 
For Loop Counting Up (Inclusive/ Close-Ended Range)
 
for (int x = 1; i <= 10; x++) {
System.out.print(x + " ");
}
 
for (val x : Int in 1 .. 10) {
print("$x ")
}
 
Kotlin uses rangeTo().
For Loop Counting Up (Exclusive/ Open-Ended Range)
 
for (int x = 1; i < 10; x++) {
System.out.print(x + " ");
}
 
for (x : Int in 1 until 10) {
print("$x ")
}
for (x : Int in 1 ..< 10) {
print("$x ")
}
 
Kotlin uses rangeUntil().
For Loop Counting Down (Inclusive/ Close-Ended Range)
 
for (int x = 10; i >= 1; x--) {
System.out.print(x + " ");
}
 
for (val x : Int in 10 downTo 1) {
print("$x ")
}
 
 
For Loop Counting Down (Exclusive/ Open-Ended Range)
 
for (int x = 10; i > 0; x--) {
System.out.print(x + " ");
}
 
for (val x : Int in 10 downTo (0+1)) {
print("$x ")
}
 
 
For Loop Array
 
for (item : array) {
System.out.println(item);
}
 
for (item in array) {
println("$item")
}
 
 
For Loop Collection
 
for (item : collection) {
System.out.println(item);
}
 
for (item in collection) {
println("$item")
}
 
 
Compare a variety of options
 
switch (option) {
case option1: {}
case option2: {}
default: {}
}
 
when (option) {
option1 -> {}
option2 -> {}
else -> {}
}
 
 
Class Inheritance
 
public class Animal {
public void eat() {
System.out.println("Animal is eating");
}
}
public class Dog extends Animal {
@java.lang.Override
public void eat() {
System.out.println("Dog is eating");
}
}
 
open class Animal {
open fun eat() : Unit {
println("Animal is eating");
}
}
class Dog : Animal() {
override fun eat() : Unit {
println("Dog is eating");
}
}
 
The class Animal is a concrete class hence can be instantiated.
Abstract Class
 
public abstract class Animal {
public void eat();
}
public class Dog extends Animal {
@java.lang.Override
public void eat() {
System.out.println("Dog is eating");
}
}
 
abstract class Animal {
open fun eat() : Unit
}
class Dog : Animal() {
override fun eat() : Unit {
println("Dog is eating");
}
}
 
The class Animal cannot be instantiated.
Interface
 
public interface Roamable {
public void roam();
}
public class Dog implements Roamable {
@java.lang.Override
public void roam() {
System.out.println("Dog is roaming");
}
}
 
interface Roamable {
fun roam() : Unit
}
class Dog : Roamable {
override fun roam() : Unit {
println("Dog is roaming");
}
}
 
 
Check if Something Belongs to Another Thing
 
painter instanceof Person
 
painter is Person
 
Implicit casting.
Assuming given objects belong to same class hierarchy.
Safe Calls (Preventing NullPointerException)
 
if (x != null) {
x.methodA();
}
 
x ?. functionA()
x ?. let {
// do stuff with x
}
x ?: y
x !!. something
 
Kotlin ?. is the "safe call" operator
Kotlin ?: is the "Elvis" operator => if (x != null) x else y
Kotlin !! operator is an assertion that variable is NOT null; if indeed null, NullPointerException is thrown
Casting
 
if (painter instanceof Person) {
Person person = (Person) painter;
}
 
if (painter is Person) {
val x : Person = painter as Person
}
 
Explicit casting.
Assuming given objects belong to same class hierarchy.
Kotlin can use as? to prevent ClassCastException.
Shallow Copy
 
something.clone()
 
something.copy()
 
Changes to the copied object also affects the original object
Exception Handling (try/catch/finally)
 
try {
// do something risky
} catch (Exception ex) {
// handle exception
} finally {
// cleanup
}
 
try {
// do something risky
} catch (ex : Exception) {
// handle exception
} finally {
// cleanup
}
 
 
Concurrency
 
Thread, Runnable
 
Coroutines
 
 
Testing
 
JUnit
 
KotlinTest
 
 
Visibility Modifiers
 
public, private, protected, default (package private)
 
public, private, protected and internal
 
 
Singleton
 
enum MySingleton {
INSTANCE;
}
 
object class MySingleton {}
 
 
Dependency Injection
 
Annotated @org.springframework.beans.factory.annotation.Autowired on the instance variables (fields)
 
Using constructor, passing variables as constructor parameters
 
 
Default Imports
 
java.lang.*
 
kotlin.*
kotlin.annotation.*
kotlin.collections.*
kotlin.comparisons.*
kotlin.io.*
kotlin.ranges.*
kotlin.sequences.*
kotlin.text.*
java.lang.* (if target against Java/JVM)
kotlin.jvm.* (if target against Java/JVM)
kotlin.js.* (if target against JavaScript)