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

From Java To Kotlin FUNCTION PDF

This document compares function syntax between Java and Kotlin. It shows that Kotlin functions use simpler syntax compared to Java by allowing features like type inference, default parameter values, single expression functions, named arguments, and destructuring declarations. Kotlin also supports data classes for grouping properties and functionality together.

Uploaded by

Mridupaban Dutta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
143 views

From Java To Kotlin FUNCTION PDF

This document compares function syntax between Java and Kotlin. It shows that Kotlin functions use simpler syntax compared to Java by allowing features like type inference, default parameter values, single expression functions, named arguments, and destructuring declarations. Kotlin also supports data classes for grouping properties and functionality together.

Uploaded by

Mridupaban Dutta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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