詳解 Kotlin Reference Basic Types, String, Array and Imports
基本數據類型
Kotlin中支持的基本數據類型及它所占Bit寬度:
| Type | Bit width |
|---|---|
| Double | 64 |
| Float | 32 |
| Long | 64 |
| Int | 32 |
| Short | 16 |
| Byte | 8 |
Char 在kotlin中 并不是一個數值類型
kotlin不支持8進制, 支持 2、10、16進制
下面的代碼,示例了:
關于2、10、16進制;
使用下劃線在數值常量賦值數據中;
使用==和===進行比較;
基本數據類型間的類型轉換方法toXxx;
位移操作;
字符,轉義符
package com.stone.basic.types/** * desc : 基本數據類型 按位操作符 * author: stone * email : aa86799@163.com * time : 30/05/2017 19 14 */fun basic() { var intValue = 7777 var floatValue1 = 8.3f var floatValue2 = 10.45F var doubleValue = 9.99 var longValue = 1L// var longValue = 1l //不能用 小寫l后綴 var hexValue = 0XA8a8a8a8a8a8a8a //Hexadecimals 0x或0X開頭 println("hexValue: ${hexValue > Int.MAX_VALUE}") var doubleValue2 = 1.3e24 //科學記數法 1.3*10^24 println("1e24 / (10^20) : ${doubleValue2 / Math.pow(10.0, 20.0)}") val binaryValue = 0B00001011 //以 0B或0b 開頭 println("binaryValue : $binaryValue") /* 不像java中 有一個基本類型 float 對應一個 裝箱類型 Float kotlin中只有 后者 kotlin中 都能 對應一個 空檢查的 裝箱類型,即后面加問號: T? */}//使用下劃線在數值常量賦值數據中,增加可讀性val oneMillion = 1_000_000val creditCardNumber = 1234_5678_9012_3456Lval socialSecurityNumber = 999_99_9999Lval hexBytes = 0xFF_EC_DE_5Eval bytes = 0b11010010_01101001_10010100_10010010fun equal() { val a: Int = 10000 val b: Int = 10000 println("1 : ${a === b}") // Prints 'true' val boxedA: Int? = a val anotherBoxedA: Int? = a println("2 : ${boxedA === anotherBoxedA}") // !!!Prints 'false'!!! println("3 : ${boxedA == anotherBoxedA}") // Prints 'true'// val c: Int? = 1// val d: Long? = c // c 不能賦值給 d// println("4 : ${c == d}") // Int 和 Long不能 相比 //像 上面這樣的 隱式轉換 都行不通的, 只能使用如下明確轉換方式: to方法 val e: Int = 1 val f: Long = e.toLong() /* - toByte(): Byte ― toShort(): Short ― toInt(): Int ― toLong(): Long ― toFloat(): Float ― toDouble(): Double ― toChar(): Char */ //類型推斷 val l = 1L + 3 // Long + Int => Long}fun bitwise() { val r = 1 shl 2 and 0x000FF000 /* bitwise operations 按位操作符: ― shl(bits)
主站蜘蛛池模板:
建水县|
富蕴县|
改则县|
利津县|
桐庐县|
揭阳市|
江口县|
岳池县|
绥德县|
南雄市|
昌邑市|
唐河县|
沁水县|
灵山县|
南澳县|
资源县|
恩施市|
浏阳市|
锡林郭勒盟|
项城市|
南开区|
兴文县|
灵石县|
彭州市|
漳平市|
齐齐哈尔市|
聂拉木县|
太保市|
南京市|
德令哈市|
安康市|
玛纳斯县|
民县|
从化市|
长葛市|
忻城县|
曲沃县|
抚远县|
鸡西市|
麦盖提县|
凤山县|