This file contains hidden or 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
| val a = "Kotlin" | |
| val b: String? = null | |
| println(a?.length) // 6 | |
| println(b?.length) // null |
This file contains hidden or 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
| val len = if (nullableGreeting != null) { | |
| nullableGreeting.length | |
| } else { | |
| 0 | |
| } |
This file contains hidden or 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
| var nullableGreeting: String? = "Hello, World" | |
| nullableGreeting = null // Works |
This file contains hidden or 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
| var greeting: String = "Hello, World" | |
| greeting = null // Compilation Error |
This file contains hidden or 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
| suspend fun getStatus(): List<String> { | |
| val currentUserDeferred = someApi.fetchUser() | |
| val currentCompanyDeferred = someApi.fetchCompany() | |
| return listOf(currentUserDeferred.await(), currentCompanyDeferred.await()) | |
| } |
This file contains hidden or 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
| async function getStatus() { | |
| const currentUserPromise = someApi.fetchUser(); | |
| const currentCompanyPromise = someApi.fetchCompany(); | |
| return await Promise.all([currentUserPromise, currentCompanyPromise]); | |
| } |
This file contains hidden or 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 Movie( | |
| val name: String, | |
| val rating: Int, | |
| val director: String | |
| ) | |
| val movie1 = Movie("Back to the Future", 5, "Bob Zemeckis") | |
| val movie1 = Movie("Star Wars: Episode IV - A New Hope", 5, "George Lucas") |
This file contains hidden or 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
| const movie1 = { | |
| name: "Back to the Future", | |
| rating: 5, | |
| director: "Bob Zemeckis" | |
| } | |
| const movie2 = { | |
| name: "Star Wars: Episode IV - A New Hope", | |
| rating: 5, | |
| director: "George Lucas" | |
| } |
This file contains hidden or 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 Monster(val name: String, val color: String, val numEyes: Int) { | |
| fun speak(likes: String):String { | |
| return "My name is $name and I like $likes" | |
| } | |
| } | |
| var nhama = Monster("Nhama", "red", 1) | |
| // Kotlin doesn't have a `new` keyword - you instantiate a class by calling it directly | |
| nhama.speak("guacamole") | |
| // "My name is Nhama and I like guacamole" |
This file contains hidden or 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 Monster { | |
| constructor(name, color, numEyes) { | |
| this.name = name; | |
| this.color = color; | |
| this.numEyes = numEyes; | |
| } | |
| speak(likes) { | |
| return `My name is ${this.name} and I like ${likes}`; | |
| } | |
| } |