This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Car { | |
private String make; | |
private String model; | |
private String registration; | |
public String getmake() { | |
return make; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//**Attempting to assign null to a regular variable** | |
var hello: String = "hello" | |
hello = null // compilation error | |
//**Null safety** | |
//By checking for Null | |
val b = "kotlin" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//* let | |
fun <T, R> T.let(f: (T) -> R): R = f(this) | |
//Scoping | |
Person.getName().let { name -> | |
print(name) | |
} | |
// name is no longer visible here | |
//To check for null values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Car(val make: String, val model: String, val registration: String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class Person { | |
String name; | |
} | |
public void printName(Person person) { | |
if (person.name != null) { | |
foo(person.name.length()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person(var name: String?) // the name property can be null | |
class Person(var name: String) // the name property cannot be null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T, R> T.let(f: (T) -> R): R = f(this) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Person.getName().let { name -> | |
print(name) | |
} | |
// name is no longer visible here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T> T.apply(f: T.() -> Unit): T { f(); return this } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Person(name).apply { foo() }; | |
//Equivalent 4 line Java code | |
Person getFoo(String name) { | |
Person person = new Person(name); | |
person.foo(); | |
return person; | |
} |
OlderNewer