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
| import kotlinx.coroutines.GlobalScope | |
| import kotlinx.coroutines.async | |
| import kotlinx.coroutines.delay | |
| import java.lang.Thread.sleep | |
| fun main() { | |
| val a = GlobalScope.async { throw RuntimeException() } | |
| val b = GlobalScope.async { delay(2000).also { println("I leaked!") } } // Will not be printed! |
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
| import kotlinx.coroutines.delay | |
| import kotlinx.coroutines.runBlocking | |
| import kotlin.system.measureTimeMillis | |
| suspend fun loadImage(name: String) { | |
| println("Loading image: $name...") | |
| delay(2000) // simulate slow behaviour | |
| println("Done loading image: $name.") | |
| } |
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
| fun main() { | |
| val address1 = Address(postalCode = "1506TX", houseNumber = "4") | |
| val address2 = address1.copy() | |
| println("address1 == address2: ${address1 == address2}") | |
| val address3 = address1.copy() | |
| address3.street = "Hermitage 4" | |
| println("address1 == address3: ${address1 == address3}") |
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 Product(val ean: String, val title: String) | |
| class Book(val publisher: String, val author: String) | |
| fun main() { | |
| val book = Book( | |
| ean = "9781617293290", | |
| title = "Kotlin in Action", | |
| publisher = "Manning", | |
| author = "Dmitry Jemerov, Svetlana Isakova" |
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
| fun main() { | |
| val product = Product() | |
| val book1 = Book(ean = "9780671628314") | |
| val book2 = Book(ean = "9781617293290", title = "Kotlin in Action") | |
| } |
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
| fun main() { | |
| val students = listOf( | |
| Student("John", 19, "UK"), | |
| Student("Clair", 16, "US"), | |
| Student("Linda", 21, "NL"), | |
| Student("Jan", 14, "NL") | |
| ) | |
| } |
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
| fun printLength(str: String?) { | |
| val length: Int = str.length | |
| print("$str length is $length") | |
| } |
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
| fun main() { | |
| val houseNumber = "121" | |
| val nextHouseNumber = houseNumber + 1 | |
| println(nextHouseNumber) // should print 122 | |
| } |
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
| import java.util.List; | |
| public class Loops { | |
| public static void main(String[] args) { | |
| List<String> list = List.of("a", "b"); | |
| for (String s : list) { | |
| System.out.println(s); | |
| } |
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
| public class IfWhen { | |
| public static boolean yesNoToBoolean(String s) { | |
| if ("yes".equals(s)) return true; | |
| else if ("no".equals(s)) return false; | |
| else throw new RuntimeException("Unsupported value: " + s); | |
| } | |
| public static void main(String[] args) { | |
| System.out.println(yesNoToBoolean("yes")); |