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 final class ExtensionFunctionKt { | |
| public static String addExclamation(String receiver) { | |
| return receiver + "!"; | |
| } | |
| } |
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 String.addExclamation(): String { | |
| return this + "!" | |
| } | |
| fun main() { | |
| val result = "skydoves".addExclamation() | |
| println(result) // Output: skydoves! | |
| } |
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
| int result = 10 * 2; |
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
| inline fun inlineHigherOrderExample(operation: (Int) -> Int): Int { | |
| return operation(10) | |
| } | |
| val result = inlineHigherOrderExample { it * 2 } |
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
| Function1<Integer, Integer> lambda = new Function1<Integer, Integer>() { | |
| @Override | |
| public Integer invoke(Integer it) { | |
| return it * 2; | |
| } | |
| }; | |
| int result = higherOrderExample(lambda); |
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 result = higherOrderExample { it * 2 } |
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
| int higherOrderExample(Function1<Integer, Integer> operation) { | |
| return operation.invoke(10); | |
| } |
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 higherOrderExample(operation: (Int) -> Int): Int { | |
| return operation(10) | |
| } |
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 id1: Any = UserId("abc") // Stored as Any, forces boxing | |
| val id2: UserId? = UserId("def") // Nullable type can force boxing | |
| val listOfIds = listOf(UserId("1")) // Generic collection, forces boxing |
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 final class IdKt { | |
| // The function takes the PRIMITIVE type directly | |
| public static final void processId(String userId) { | |
| String var1 = "Processing user with ID: " + userId; | |
| System.out.println(var1); | |
| } | |
| public static final void main() { | |
| // No UserId object allocated | |
| String myId = UserId.constructor_impl("user-123"); |
NewerOlder