목록2024/03 (3)
Hanbit the Developer
배경 hashCode()는 주로 해시 기반의 컬렉션에서 객체를 저장하고 검색하는 데 사용됩니다. 오늘은 data class, Integer, Boolean, String에서 이 함수가 각각 어떻게 구현되었는지 알아보고자 합니다. hashCode() for data class data class User( val id: Int, val age: Int, val isStudent: Boolean, val firstName: String, val lastName: String, ) { init { hashCode() } } 위 코틀린 data class를 디컴파일 하면 아래와 같은 자바 코드가 생성됩니다. public int hashCode() { int var10000 = ((Integer.hashCode(..
배경 Java에서 List의 대표적인 구현체인 ArrayList와 LinkedList의 구현을 알아보겠습니다. ArrayList ArrayList는 MutableList의 기본 구현체이다. 코틀린에서 mutableListOf() 함수를 사용하면 ArrayList가 기본 구현체로서 반환됩니다. /** * Returns an empty new [MutableList]. * @sample samples.collections.Collections.Lists.emptyMutableList */ @SinceKotlin("1.1") @kotlin.internal.InlineOnly public inline fun mutableListOf(): MutableList = ArrayList() @SinceKotlin("1..
배경 개인적으로 Retrofit2의 구현을 분석한 적이 있는데 이때 suspend fun이 디컴파일될 때 마지막 인자로 Continuation이 붙으면서 처리된다는 것을 알게 되었고, 원리를 직접 알아보고 싶었습니다. 예시 1: 간단한 코루틴 다음과 같은 간단한 예시를 작성하였습니다. 기존 코드 class SampleViewModel : ViewModel() { fun logout() { viewModelScope.launch { val userId = getUserId() logoutUser(userId) } } private suspend fun getUserId(): Int = 0 private suspend fun logoutUser(userId: Int) { Log.d("SampleViewMod..