Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

9/17/2019 From Java to Kotlin

From Java to Kotlin

Basic Functions Classes

FUNCTIONS

Basic Function

Java Kotlin
public void hello() { fun hello() {
System.out.print("Hello, World!"); println("Hello, World!")
} }

Arguments

Java Kotlin
public void hello(String name){ fun hello(name: String) {
System.out.print("Hello, " + name + "!"); println("Hello, $name!")
} }

Default Values
https://fabiomsr.github.io/from-java-to-kotlin/functions.html 1/6
9/17/2019 From Java to Kotlin

Java Kotlin
public void hello(String name) { fun hello(name: String = "World") {
if (name == null) { println("Hello, $name!")
name = "World"; }
}

System.out.print("Hello, " + name + "!");


}

Return

Java Kotlin
public boolean hasItems() { fun hasItems() : Boolean {
return true; return true
} }

Single-Expression

Java Kotlin
public double cube(double x) { fun cube(x: Double) : Double = x * x * x
return x * x * x;
}

https://fabiomsr.github.io/from-java-to-kotlin/functions.html 2/6
9/17/2019 From Java to Kotlin

FUNCTIONS

Vararg

Java Kotlin
public int sum(int... numbers) { } fun sum(vararg x: Int) { }

Main

Java Kotlin
public class MyClass { fun main(args: Array<String>) {
public static void main(String[] args){
}
}
}

Named Arguments

Java Kotlin

https://fabiomsr.github.io/from-java-to-kotlin/functions.html 3/6
9/17/2019 From Java to Kotlin

public static void main(String[]args){ fun main(args: Array<String>) {


openFile("file.txt", true); openFile("file.txt", readOnly = true)
} }

public static File openFile(String filename, boolean readOnly) { } fun openFile(filename: String, readOnly: Boolean) : File { }

Optional Arguments

Java Kotlin
public static void main(String[]args){ fun main(args: Array<String>) {
createFile("file.txt"); createFile("file.txt")

createFile("file.txt", true); createFile("file.txt", true)


createFile("file.txt", appendDate = true)
createFile("file.txt", true, false);
createFile("file.txt", true, false)
createExecutableFile("file.txt"); createFile("file.txt", appendDate = true, executable = true)
}
createFile("file.txt", executable = true)
public static File createFile(String filename) { } }

public static File createFile(String filename, boolean appendDate) { } fun createFile(filename: String, appendDate: Boolean = false,
executable: Boolean = false): File { }
public static File createFile(String filename, boolean appendDate,
boolean executable) { }

public static File createExecutableFile(String filename) { }

Generic Methods

Java Kotlin
https://fabiomsr.github.io/from-java-to-kotlin/functions.html 4/6
9/17/2019 From Java to Kotlin

public void init() { fun init() {


List<String> moduleInferred = createList("net"); val module = createList<String>("net")
} val moduleInferred = createList("net")
}
public <T> List<T> createList(T item) { }
fun <T> createList(item: T): List<T> { }

Data Classes - Destructuring

Java Kotlin
public static void main(String[]args) { fun main(args: Array<String>) {
Book book = createBook(); val book = createBook();
// or
System.out.println(book); val (title, author) = createBook()
System.out.println("Title: " + book.title);
} println(book)
println("Title: $title")
public static Book createBook(){ }
return new Book("title_01", "author_01");
} fun createBook() : Book{
return Book("title_01", "author_01")
public class Book { }
final private String title;
final private String author; data class Book(val title: String, val author: String)

public Book(String title, String author) {


this.title = title;
this.author = author;
}

public String getTitle() {


return title;
}

public String getAuthor() {


return author;
}

https://fabiomsr.github.io/from-java-to-kotlin/functions.html 5/6
9/17/2019 From Java to Kotlin
@Override
public String toString() {
return "Title: " + title + " Author: " + author;
}
}

https://fabiomsr.github.io/from-java-to-kotlin/functions.html 6/6

You might also like