Kotlin vs Java
public void hello(String name) {
if (name == null) {
name = "World";
}
System.out.print("Hello, " + name + "!");
}
fun hello(name: String = "World") {
println("Hello, $name!")
}
public double cube(double x) {
return x * x * x;
}
fun cube(x: Double) : Double = x * x * x
public int sum(int... numbers) { }
fun sum(vararg x: Int) { }
public class Main {
public static void main(final String[] args){
openFile("file.txt", true);
}
}
class FileUtils {
public static File openFile(String filename, boolean readOnly) { }
}
fun main() {
openFile("file.txt", true)
}
fun openFile(filename: String, readOnly: Boolean) : File { }
public static void main(String[]args){
createFile("file.txt");
createFile("file.txt", true);
createFile("file.txt", true, false);
createExecutableFile("file.txt");
}
public static File createFile(String filename) { }
public static File createFile(String filename, boolean appendDate) { }
public static File createFile(String filename, boolean appendDate,
boolean executable) { }
public static File createExecutableFile(String filename) { }
fun main(args: Array<String>) {
createFile("file.txt")
createFile("file.txt", true)
createFile("file.txt", appendDate = true)
createFile("file.txt", true, false)
createFile("file.txt", appendDate = true, executable = true)
createFile("file.txt", executable = true)
}
fun createFile(filename: String, appendDate: Boolean = false,
executable: Boolean = false): File { }
public void init() {
var moduleInferred = createList("net");
}
public <T> List<T> createList(T item) { }
fun init() {
val module = createList<String>("net")
val moduleInferred = createList("net")
}
fun <T> createList(item: T): List<T> { }
public static void main(String[]args) {
val book = createBook();
System.out.println(book);
System.out.println("Title: " + book.title);
}
public static Book createBook(){
return new Book("title_01", "author_01");
}
@Data // Lombok
public class Book {
private final String title;
private final String author;
}
fun main(args: Array<String>) {
val book = createBook();
// or
val (title, author) = createBook()
println(book)
println("Title: $title")
}
fun createBook() : Book{
return Book("title_01", "author_01")
}
data class Book(val title: String, val author: String)