본문 바로가기

# 02

[Kotlin] Class initializer java - constructor (생성자) public class UserInfo {private String name;private int age;private String birthday; public UserInfo(String name) {this.name = name;} public UserInfo(String name, int age) {this(name);this.age = age;} public UserInfo(String name, int age, String birthday) {this(name, age);this.birthday = birthday;}} kotlin - constructor (생성자) class UserInfo {var name : String = ""var age ..
[Kotlin] Class Inheritance open class 상속 open class Empty class Sample : Empty() java View class 상속 /*open*/ class CustomConstraintLayout (context : Context,attrs : AttributeSet?,defstyleAttr : Int): ConstraintLayout(context, attrs, defStyleAttr) { constructor(context : Context, attrs : AttributeSet?) : this (context, attrs, 0) constructor(context : Context) : this (context, null)} - default 값을 추가하여 secondary 생성자를 제거한다. /..
[Kotlin] Null 예외처리 - etc 안전한 형 변환 - java Object object = "name";int index = (int) object;System.out.println("index " + index); -> java.lang.ClassCastException 발생 - instanceof 추가하여 안전하게 처리 Object object = "name";int index = 0;if( object instanceof Integer) { // object 값이 Integer인지 확인하고 맞으면 변환하므로 안전한 방법임!!index = (int) object;}System.out.println("index " + index); 코틀린 안전한 형변환 시도하기 val a : Any? = "ABC"val b : Int? = a as I..
[Kotlin] Null 예외처리 if/else 대신 사용하기 - java -> java에서는 3항 식을 사용할 수 있다. String temp = null;int size = temp != null ? temp.length() : 0; if/else 대신 사용하기 - kotlin var temp : String? = null// val size = if (temp != null) temp.length else 0 ->val size = temp?.length var temp : String? = ""val size = temp?.length - 결과 : length 또는 null Elvis Operator을 사용하여 null인 경우 값을 지정하자 - Elvis Operator : ?: var temp : String? = null// ..
[Kotlin] Null 처리 방법 java - null을 허용한다.- Android Annotation을 이용하여 @NonNull과 @Nullable을 지정할 수 있다. Null 처리 메소드 - java public void set(@NonNull String a, @Nullable String b) {if(a == null) throw new NullPointerException("a is null"); // a가 null일 시 NullPointerException을 발생시킬 수 있다.System.out.println("a " + a + ", b " +b);} -> Annotation을 사용할 수 있지만 다음 코드를 사용할 수 있다.set(null, null);// 빌드시 에러 안생김. kotlin - 기본적으로 null을 허용하지 않..
[Kotlin] return and jumps and This-expressions Jump Expression - return // 값을 반환- break // 루틴을 빠져나갈 때- continue // 특정 조건에서만 처리하지 않을 때 Labels 정의 loop@ for (i in 1..100) {// ...} -> label을 정의할 때는 name@-> label을 사용할 때는 @name Break and Continue Labels for (i in 1.. 100) {for (j in 1.. 100) {if (j > 10) breakprint( j )}println()}println("end") -> label 추가for (i in 1..100) {loop@ for (j in 1..100) {if (j > 10) break@loopprint(j)}println()}println(..
[Kotlin] Lambda Lambdas -> javabutton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick (View view) {view.setAlpha(0.5f);}}); -> kotlinbutton.setOnClickListener {view -> view.alpha = 0.5f}-> view 생략가능 파라미터가 한개인 경우button.setOnClickListener {it.alpha = 0.5f} it : implicit name of a single parameter Lambdas Closures -> javaint count = 0;Button button = findViewById(R.id.button);button.set..
[Kotlin] Control Flow - if/when/loops if fun test() {val a = 10val b = 20var max = aif ( a b ) a else b} fun test() {val a = 10val b = 20val max : Intif ( a > b ) {max = a} else {max = b}} fun test() {val a = 10val b = 20val max : Intval min : Intif ( a > b ) {max = amin = b} else {max = bmin = a}}println("max $max min $min") java switch int a = 10;switch (a) ..