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"); |
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
| @JvmInline | |
| value class UserId(val id: String) | |
| fun processId(userId: UserId) { | |
| println("Processing user with ID: ${userId.id}") | |
| } | |
| fun main() { | |
| val myId = UserId("user-123") | |
| processId(myId) |
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
| // simplified | |
| override val value: T | |
| get() { | |
| if (_value !== UNINITIALIZED_VALUE) { | |
| return _value as T | |
| } | |
| val initializerValue = initializer | |
| if (initializerValue != null) { | |
| val newValue = initializerValue() | |
| if (valueUpdater.compareAndSet(this, UNINITIALIZED_VALUE, newValue)) { |
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
| // simplified | |
| override val value: T | |
| get() { | |
| if (_value !== UNINITIALIZED_VALUE) { | |
| return _value as T // Fast path: no lock needed | |
| } | |
| return synchronized(lock) { | |
| if (_value !== UNINITIALIZED_VALUE) { | |
| _value as T // Second check inside lock | |
| } else { |
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
| // simplified | |
| override val value: T | |
| get() { | |
| if (_value === UNINITIALIZED_VALUE) { | |
| _value = initializer!!() | |
| initializer = null | |
| } | |
| return _value as T | |
| } |