1. 程式人生 > >kotlin基礎資料型別

kotlin基礎資料型別

kotlin基礎資料型別


//Number型別
//Double 64
//Float 32
//Long 64
//Int 32
//Short 16
//Byte 8
//執行在虛擬機器中 長度和作業系統無關
val aInt:Int = 8//java int Integer合體 自動進行區分
val anotherInt:Int=0xFF
val moreInt:Int = 0b00000001
val maxInt:Int = Int.MAX_VALUE
val minInt:Int=Int.MIN_VALUE

val aLong:Long=12345632223223
val another:Long=123
val maxLong:Long = Long.MAX_VALUE
val minLong:Long = Long.MIN_VALUE

val aFloat:Float = 2.0f
val anotherFloat=1E3f
val maxFloat = Float.MAX_VALUE
val minFloat = -Float.MAX_VALUE

val aDouble:Double = 3.0//雙精度浮點型
val maxDouble = Double.MAX_VALUE
val minDouble = -Double.MAX_VALUE

val aShort = 127
val maxShort = Short.MAX_VALUE//32767
val minShort = Short.MIN_VALUE//-32768

val byte:Byte = 127//-128-127

//Char對應java 中的Character類
//Char佔兩個位元組 表示一個16位的unicode字元
//字元用單引號'' 例如'a' '/n'
val aChar:Char = '0'
val bChar:Char='中'
val cChar:Char='\u900f'//unicode編碼
//轉義字元
// \t製表符  \b回退符 \n回車 \r游標回到行首  \'單引號 \"雙引號 \\反斜槓 \$美元符號
//$開頭在kotlin 中是字串模板

//不可以隱士轉換
val errorInt:Int = 5
val aLong1:Long = errorInt.toLong()
//val aLong2:Long = errorInt //type mismatch

val aString:String = "hello"
val fromChars:String = String(charArrayOf('h','e','l','l','o'))

fun main(args:Array<String>){
    println(anotherInt)
    println(moreInt)
    println(123L)

    println(maxInt)
    println(Math.pow(2.0,31.0)-1)

    println(minInt)
    println(-Math.pow(2.0,31.0))

    println(maxLong)
    print(Math.pow(2.0,63.0)-1)
    println(minLong)
    println(-Math.pow(2.0,63.0))

    println(aFloat)
    println(anotherFloat)

    println(0/0.0f == Float.NaN)//返回false NaN不能比較
    //Float.NaN

    println(aChar+" "+ bChar+" "+ cChar)

    println(aString == fromChars)//kotlin == 和 equals等價的
    println(aString === fromChars)//物件引用值比較

    println("輸出:"+ aString)

    val arg1:Int = 0
    val arg2:Int = 1
    println(""+arg1+" + "+arg2+" = "+(arg1+arg2))//java 字串寫法
    println("$arg1 + $arg2 = ${arg1+arg2}")//kotlin模板寫法

    //
    val sayHello:String = "Hello \"Trumph\""
    println(sayHello)
    println("\$$arg1")
    val rawString:String = """//打印出來原始字串 無法進行轉義
        \t
        \n
        \\\\$arg1
                \\\\ $ arg1

        """
    println(rawString.length)