Kotlin for Java Developers in 10 minutes

Spread the love

Kotlin is a statically typed programming language that runs on Java virtual machine and also can be compiled to JavaScript source code. It is designed to interoperate with Java code, meaning Kotlin code can be called from Java and Java code from Kotlin.

Variables

Java

final int x;
final int y = 1;
int z = 2;

Kotlin

val x: Int
val y = 1
var z = 2

Nullable Property

Java

final String name = null;
String lastName;
lastName = null

Kotlin

val name: String? = null
var lastName: String?
lastName = null
var firstName: String
// Compilation error!!
firstName = null

Null Safety

Java

if (name != null) { 
int length = name.length
}

Kotlin

val length = name?.length

Multi Line String

Java

String address = "FirstLine\n" +
"Second Line\n" +
"Third Line";

Kotlin

val address = """
|FirstLine
|SecondLine
|ThirdLine
""".trimMargin()

Ternary Operator

Java

String result = x > 5 ? "x > 5" : "x<=5";

Kotlin

val result = if (x > 5)
"x > 5"
else "x <=5"

Switch/When

Java

final int x = 3;
final String result;
switch (x) {
case 0:
case 11:
result = "0 or 11"
break;
case 1:
case 2:
//...
case 10:
result = " from 1 to 10";
break;
default
result = " not within range 0-11"

Kotlin

val x = 3
val result = when (x) {
0,11 -> "0 or 11"
in 1..10 -> " from 1 to 10"
else -> "not within range 0-11"
}

Collections

Java

final List<Integer> numbers = Arrays.asList(1, 2, 3);
final Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "One");
map.put(2, "Two);
map.put(3, "Three);

Kotlin

val numbers = listof(1, 2, 3)
val map = mapOf(1 to "One",
2 to "Two",
3 to "Three")

Functions

Vararg

Java

public int sum(int... numbers ) {}

Kotlin

fun sum(vararg x: It) {}

Main

Java

public class MyClass { 
public static void main(String[] args) {
}
}

Kotlin

fun main(args: Array<String>){
}

Optional Arguments

Java

openFile("file.txt", true);
public static File openFile(String filename, boolean readonly);

Kotlin

openFile("file.txt")
fun openFile(filename: String, readOnly: Boolean = true)

Classes

Constructor Call

Java

final User user = new User("Tom");

Kotlin

val user = User("Tom")

Final Class

Java

public final class User {
}

Kotlin

class User

Open Class

Java

public class User {
}

Kotlin

open class User

Final Attributes

Java

final class User {
private final String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

Kotlin

class User(val name: String)

Primary Constructor

Java

final class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Kotlin

class User(var name: String)

Optional arguments in Constructors

Java

final class User {
private String name;
private String lastName;

public User(String name) {
this(name, "");
}
public User(String name, String lastName) {
this.name = name;
this.lastName = lastName;
}
// Getter and setters
}

Kotlin

class User(var name: String, var lastName: String = "")

Properties

Java

public class User {
private String id = "00x";

public String getId() {
return id;
}
public void setId(String id) {
if(id !=null && !id.isEmpty()){
this.id = id;
}
}

}

Kotlin

class User {
var id: String = "00x"
set(value) {
if(value.isNotEmpty()) field = value
}
}

Abstract Class

Java

public abstract class Animal {
public abstract void sound();
}
public class Dog extends Animal {
@override
public void sound() {
System.out.println("Woof");
}
}

Kotlin

abstract class Animal {
abstract fun sound(): Unit
}
class Dog : Animal() {
override fun sound(): Unit {
println("Woof")
}
}

Singleton

Java

public class Animal {
private static final Animal instance = new Animal();

public static Animal getInstance(){
return instance;
}
}

Kotlin

object Animal {
}

Extensions

Java

public class ByteArrayUtils { 
public static String toHexString(byte[] data) {
}

}
final byte[] dummyData = new byte[10];
final String hexValue = ByteArrayUtils.toHexString(dummyData);

Kotlin

fun ByteArray.toHex() : String {
}
val dummyData = byteArrayOf()
val hexValue = dummyData.toHex()

Interface

Java

public interface Animal {
void eat();
}
public class Dog implements Animal {
@Override
public void eat() {
}
}

Kotlin

interface Animal {
fun eat()
}
class Dog : Animal {
override fun eat() {
}
}

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *