2-1 Kotlin

SFR1811·2022년 1월 10일
0

CS349-User Interface

목록 보기
2/8

Java에 대한 온전한 대체가 가능 (Java 모듈과 혼용 가능)

컴파일: Kotlinc xx.kt
결과물: xx.class

kotlin xx 커맨드로 실행 가능


Kotlin is a strongly typed language.
변수 선언 시 암시적 타입 지정 지원
예) var a = "asdf" 입력 시 a는 string타입으로 지정


많은 경우 Casting 없이 컴파일러가 문맥상 해당 변수의 타입을 추론하는 경우가 있다.

fun foo(x: Any){
	if(x is String){
    	print(x.length) // x is automatically cast to String
    }
}

var is for regular variables.
val is for consts.

It is recommended to use val whenever it can be used


if... then statement can either be statement and expression

var voo = if (condition)
    "yes"
  else
    "no"

same can be done with when (== switch) statement


Destructuring example

val map = mapOf(Pair(1,'a'), Pair(2,'b'), Pair(3,'c'))
for ((k,v) in map){
	print("$k: $v")
}

profile
3B CS

0개의 댓글