본문 바로가기

# 02/코틀린

[Kotlin] Types

반응형

Types 및 형 변환


Double

Float

Long

Int

Short

Byte

String



값의 비교 - java 에서는 == 메모리 비교


val a : Int = 10000

print (a === a)                // Prints 'true'

val boxedA : Int? = a

val anotherBoxedA : Int? = a

print(boxedA === anotherBoxedA)          // !!!Prints 'false'!!!



값의 비교 - equals


val a : Int = 10_000

print(a ==a)                                                  // prints 'true'

val boxedA : Int? = a

val anotherBoxedA : Int? = a

print(boxedA == anotherBoxedA)                       // Prints 'true'



형 변환


toByte() : Byte

toShort() : Short

toInt() : Int

toLong() : Long

toFloat() : Float

toDouble() : Double

toChar() : Char



val a : Int? = 1

val b : Long? = a                                // Type mismatch : interred type is Int but Long was expected

// ->

val a : Int? = 1

val b : Long? = a?.toLong()


val a : Long? = 0

val b : Int? = a                                  // Type mismatch : interred type is Int but Long was expected    

// ->

val a : Long? = 0

val b : Int? = a?.toInt()



var name = "Name"

name = 0                                        // The integer literal does not conform to the expected type String

// ->

var name = "Name"

name = 0.toString()

// or

name = "0"



String 한자씩 출력 - java


java


String name = "abc";

for (int i = 0; i < name.length(); i++) {

System.out.println("charAp(" + name.charAt(i) + ")");

}


kotlin


val name = "abc"

for (ch in name)

println("charAp($ch)")

}



StringTemplates - java


java

 

System.out.println("charAp(" + name.charAt(i) + ")");


kotlin


println("charAp($ch)")

// "list $list" or "listSize : ${list.size}"

반응형

'# 02 > 코틀린' 카테고리의 다른 글

[Kotlin] Class  (0) 2019.07.11
[Kotlin] Properties and Fields  (0) 2019.07.11
[Kotlin] 조건문  (0) 2019.07.10
[Kotlin] 함수와 변수의 범위  (0) 2019.07.10
[Kotlin] android extentions  (0) 2019.07.09