So sánh cách sử dụng Function giữa java và kotlin.
1.Basic Function
public void hello() { System.out.print("Hello, World!"); }
fun hello() { println("Hello, World!") }
2.Arguments
public void hello(String name){ System.out.print("Hello, " + name + "!"); }
fun hello(name: String) { println("Hello, $name!") }
3.Default Values
public void hello(String name) { if (name == null) { name = "World"; } System.out.print("Hello, " + name + "!"); }
fun hello(name: String = "World") { println("Hello, $name!") }
4.Return
public boolean hasItems() { return true; }
fun hasItems() : Boolean { return true }
5.Single-Expression
public double cube(double x) { return x * x * x; }
fun cube(x: Double) : Double = x * x * x