Kotiln 변수 & 함수

2017. 12. 27. 03:57Work/Kotlin

728x90
반응형

변수 (val : 상수, var : 변수)

// val : 상수
// var : 변수
val int_a : Int = 1 // Default 형태(초기화)
val int_b = 2 // Int 형태(컴파일러가 유추)
val c: Int // 초기화를 늦게
c = 10

var x = 5 // 변수 선언, Int 형태(컴파일러가 유추), val로 선언시 에러
x += 1
val int_type = 10   // Int형태 (4byte)
val long_type = 20L // long형태(8byte)
val float_type = 12.2F // float형태(4byte)
val double_type = 42.2 // double형태(8byre)

함수

// 함수 : 2개의 합계를 구한다.
// 완성 형태
fun sum(a: Int, b: Int) : Int{
return a+b
}
// 간추린 형태
fun sumshort(a: Int, b: Int) = a+b
// return 값이 없을때 : kotlin.Unit 결과가 나온다. kotlin.Unit = void type
fun sumnoreturn(a: Int, b: Int) {
a+b
}
// Unit Type 생략한 결과
// Unit에 대한 자세한 내용 설명은 https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html 참조
// Unit : java의 void 유형에 해당
fun printsum(a: Int, b: Int) {
println(a+b)
}
// Unit Type
fun printsumunit(a: Int, b: Int) : Unit {
println(a+b)
}
// Default Type 함수
fun sumdefaulttype(a: Int = 10, b: Int =20) : Int{
return a+b
}


728x90
반응형

'Work > Kotlin' 카테고리의 다른 글

Kotlin 클래스  (0) 2017.12.31
Kotlin 반복  (0) 2017.12.29
Kotlin NPE(Null Pointer Exception)  (0) 2017.12.28
Kotlin 배열  (0) 2017.12.27
Kotiln 설치 및 설정  (0) 2017.12.12