{ return 4 } fun returnsAFour()= 4 fun sum(x: Int, y: Int, z: Int = 0, w: Int = 0) = x + y + z + w sum(x = 1, y = 0, z = 4, w = 6) void can be inferred default value named argument
private int age; @Nullable public final String getName() { return this.name; } public final void setName(@Nullable String var1) { this.name = var1; } public final int getAge() { return this.age; } public final void setAge(int var1) { this.age = var1; } public Person(@Nullable String name, int age) { this.name = name; this.age = age; } } class Person(name: String?, age: Int) { var name = name var age = age }
private int age; @NotNull public final String getName() { return this.name; } public final void setName(@NotNull String var1) { Intrinsics.checkParameterIsNotNull(var1, "<set-?>"); this.name = var1; } public final int getAge() { return this.age; } public final void setAge(int var1) { this.age = var1; } public Person(@NotNull String name, int age) { Intrinsics.checkParameterIsNotNull(name, "name"); super(); this.name = name; this.age = age; } } class Person(var name: String, var age: Int) Kotlin class has only property ( with getter and setter)
private final int age; @NotNull public final String getName() { return this.name; } public final void setName(@NotNull String var1) { Intrinsics.checkParameterIsNotNull(var1, "<set-?>"); this.name = var1; } public Person(@NotNull String name, int age) { Intrinsics.checkParameterIsNotNull(name, "name"); super(); this.name = name; this.age = age; } } class Person(var name: String, private val age: Int) mark private if you want to hide it no new constructor
name; public static final Author INSTANCE; @NotNull public final String getName() { return name; } public final void setName(@NotNull String var1) { Intrinsics.checkParameterIsNotNull(var1, "<set-?>"); name = var1; } private Author() { INSTANCE = (Author)this; name = "Nevin"; } static { new Author(); } } object Author { var name ="Nevin" } singleton println(Author.name) usage
b -> a + b } fun op(num1: Int, num2: Int, func: (Int, Int) -> Int) { val answer = func(num1, num2) } Higher-Order Function input output explicit type take functions as parameter
a, b -> a + b } fun op(num1: Int, num2: Int, func: (Int, Int) -> Int) { val answer = func(num1, num2) } op(3, 4, { a: Int, b: Int -> a + b }) val sum = { a: Int, b: Int -> a + b }