Kotlin 9

[Kotlin] open, internal

open - 코틀린의 클래스와 메서드는 기본적으로 final 따라서 상속을 허용하려면 해당 클래스 앞에 open 변경자를 붙여야함 - 오버라이드를 허용하고 싶은 메서드나 프로퍼티 앞에도 open 변경자를 붙여야함 - open class : 다른 클래스에서 상속 할 수 있음 open method : 해당 메서드를 하위 클래스에서 override 할 수 있음 internal - 자바는 public, protected, private 변경자가 있음 - 코틀린은 아무 변경자가 없는 경우 모두 public - 자바의 기본 가시성인 패키지 전용은 코틀린에 없음 ( 코틀린은 패키지를 네임스페이스 관리를 위한 용도로 사용 ) - 코틀린의 internal 은 같은 모듈 내에서만 볼 수 있음 - public : 모든 곳에..

Kotlin 2021.09.28

[Kotlin] 시프트 연산자 (shr, shl, ushr)

- shr (shift Right) * Right shift 연산 (>>) * bit값을 오른쪽으로 이동 * 부호를 유지하면서 지정한 수만큼 비트를 오른쪽으로 이동 이동으로 인한 빈자리는 부호값으로 채움 (음수:1, 양수:0) - shl (shift LEFT) * Left shift 연산 (> * bit 값을 오른쪽으로 이동 * 지정한 수만큼 비트를 오른쪽으로 이동시키며 새로운 비트는 0 println(1 shl 2) println(3 shr 2) println(4 ushr 5) 참고 : https://codedragon.tistory.com/7998

Kotlin 2021.09.16

[Kotlin] const

- val 은 값 자체를 바꿀 수 없지만 특정 클래스의 객체가 들어가면 해당 값의 속성은 바꿀 수 있음 : 속성도 변하지 않게 하고싶으면 const - val 는 런타임 시 할당되며 const val 는컴파일 시 할당 - val 는 기본 타입과 참조 타입을 넣을 수 있으며 val const는 원시 타입(기본 타입)과 String만 넣을 수 있는 완전 상수 - const 는 클래스의 속성이나 지역변수로는 사용할 수 없음 tip : 변수는 메모리를 다양하게 사용하여 메모리 차지가 많아지므로 상수를 사용하면 성능이 좋아짐 참고 : https://velog.io/@jaeyunn_15/javakotlin-%EC%BD%94%ED%8B%80%EB%A6%B0-const https://jhdroid.tistory.com..

Kotlin 2021.09.16

[Kotlin] ? 물음표 / !! 느낌표 두개 (null 처리)

- 코틀린에서는 자바보다 null 처리를 더 명확하게 하기 때문에 NPE(NullPointerException) 발생빈도를 낮출 수 있음 - ? 와 !!는 null의 사용과 관련되어 있는 문자 - ? : null이 들어올 수 있는 경우 var nameErr: String = null //오류 var naem: String? = null //정상 - !! : null 이 절대 들어오면 안되는 경우 (강제로 null이 아님을 선언 하므로 null을 넣으면 NPE발생) 참고 : https://taetoungs-branch.tistory.com/73

Kotlin 2021.09.15

[Kotlin] copyOf, toArray, contentToString (배열 복사)

- copyOf : arrayOf 배열을 복사할 때 사용 - toArray : ArrayList 배열을 복사할 때 사용 - contentToStrung :배열에 저장된 데이터 출력할 때 사용 //arrayOf 변수 선언 실시 var arr_one = arrayOf(1,2,3,4,5) println("arrayOf 원본 : "+arr_one.contentToString()) //arrayOf 원본 : [1, 2, 3, 4, 5] //arrayOf 전체 데이터 복사 실시 var arr_full_copy = arr_one.copyOf() println("arrayOf 전체 복사 : "+arr_full_copy.contentToString()) //arrayOf 전체 복사 : [1, 2, 3, 4, 5] //a..

Kotlin 2021.09.10

[Kotlin] Property / Backing Field

- 코틀린의 프로퍼티는 자동으로 게터와 세터를 구현 - 필드(field) : 클래스 내의 맴버변수 (코틀린에서는 뒷받침하는 필드, Backing Fields라고 부름) - 프로퍼티(property) : 필드와 게터 세터를 한데 묶어서 부르는 단어 - 프로퍼티의 선언 키워드에 따라 생성되는 접근자 메서드(getter/setter)가 다름 * val : 불변 값이므로 getter가 자동으로 구현 * var : 가변 값이므로 getter와 setter가 자동으로 구현 - 코틀린에서는 getter와 setter를 통하지 않고 직접 프로퍼티에 접근 * 자바 : SampleClass sample = new SamlpeClass(); sample.getName(); * 코틀린 : val sample = SamlpeC..

Kotlin 2021.09.10

[Kotlin] 기본 문법

#1. File> New> Import Project #2. 어디서 오류가 났는지 확인하고 싶다면 토스트메시지 사용하기! val toast = Toast.makeText(this, "", Toast.LENGTH_SHORT) toast.show() #3. 기본 버튼 사용 final Button button = findViewById(R.id.button_id); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { } }); #4. if문이나 switch문과 비슷한 when문 val diceImage: ImageView = findViewById(R.id.imageView) when (diceRoll) { 1..

Kotlin 2021.02.21

[Android Basics: Introduction to Kotlin] Kotlin에서 생일 메시지 만들기

fun main() { val age = 24 val layers = 5 printCakeCandles(age) printCakeTop(age) printCakeBottom(age, layers) } fun printCakeCandles(age: Int) { print (" ") repeat(age) { print(",") } println() // Print an empty line print(" ") // Print the inset of the candles on the cake repeat(age) { print("|") } println() } fun printCakeTop(age: Int) { repeat(age + 2) { print("=") } println() } fun printCake..

Kotlin 2021.02.13