詳解 android/252433.html">Kotlin Reference Basic Types, String, Array and Imports
基本數(shù)據(jù)類型
Kotlin中支持的基本數(shù)據(jù)類型及它所占Bit寬度:
| Type | Bit width |
|---|---|
| Double | 64 |
| Float | 32 |
| Long | 64 |
| Int | 32 |
| Short | 16 |
| Byte | 8 |
Char 在kotlin中 并不是一個數(shù)值類型
kotlin不支持8進(jìn)制, 支持 2、10、16進(jìn)制
下面的代碼,示例了:
關(guān)于2、10、16進(jìn)制;
使用下劃線在數(shù)值常量賦值數(shù)據(jù)中;
使用==和===進(jìn)行比較;
基本數(shù)據(jù)類型間的類型轉(zhuǎn)換方法toXxx;
位移操作;
字符,轉(zhuǎn)義符
package com.stone.basic.types/** * desc : 基本數(shù)據(jù)類型 按位操作符 * 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 //科學(xué)記數(shù)法 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 對應(yīng)一個 裝箱類型 Float kotlin中只有 后者 kotlin中 都能 對應(yīng)一個 空檢查的 裝箱類型,即后面加問號: T? */}//使用下劃線在數(shù)值常量賦值數(shù)據(jù)中,增加可讀性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不能 相比 //像 上面這樣的 隱式轉(zhuǎn)換 都行不通的, 只能使用如下明確轉(zhuǎn)換方式: 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) – signed shift left (Java's << ) — shr(bits) – signed shift right (Java's >> ) — ushr(bits) – unsigned shift right (Java's >>> ) — and(bits) – bitwise and (&) — or(bits) – bitwise or (|) — xor(bits) – bitwise xor (^) — inv() – bitwise inversion (!) */}fun charOperation() { val str = "stone" for (c in str) { println("char in str : $c") val r = c + 3// if (r == 118) {//不能如此操作:Char 在kotlin中 并不是一個數(shù)值類型// println(r)// } if (r.toInt() == 118) {//可以用toInt() 來進(jìn)行比較 println("符合條件的字符$r, 原始字符串的字符是${r - 3}") } fun decimalDigitValue(c: Char): Int { if (c !in '0'..'9') throw IllegalArgumentException("Out of range") return c.toInt() - '0'.toInt() // Explicit conversions to numbers }// decimalDigitValue('x') decimalDigitValue('6') } /* 類似'1'這樣單引號中一個字符的定義就是一個 Char 支持使用/轉(zhuǎn)義: /t , /b , /n , /r , /' , /" , // and /$ Unicode字符: '/uFF00' */}fun booleanOperation() { val b: Boolean = !true /* 支持的操作符: || 、 && 、 ! */}fun main(args: Array<String>) { basic() equal() charOperation()}String Type
package com.stone.basic.types/** * desc : * author: stone * email : aa86799@163.com * time : 30/05/2017 20 48 */fun main(args: Array<String>) { /* 使用三個雙引號開頭與結(jié)尾, 中間可以包含 任何 非轉(zhuǎn)義字符 */ val text = """|Tell me and I forget.|Teach me and I remember.|Involve me and I learn.|(Benjamin Franklin)>admin""".trim().trimMargin().trimMargin(">") //trimMargin 去掉前綴,默認(rèn)以|作margin前綴,也可以指定前綴 println(text) var s = "abc" var str = "$s.length is ${s.length}" val price = """${'$'}9.99${"/t"}一杯果汁""" println(price)}Array Type
package com.stone.basic.typesimport java.util.*/** * desc : * author: stone * email : aa86799@163.com * time : 30/05/2017 20 48 */fun main(args: Array<String>) { val ary = arrayOf(1, 3, 2) //使用arrayOf 創(chuàng)建數(shù)組// val asc = Array(5, { i -> (i * i).toString() }) val asc = Array(5, { i -> Math.random() }) //使用構(gòu)造函數(shù)創(chuàng)建數(shù)組:后面的lambda參數(shù),表示設(shè)置每個index上的元素 for (it in asc) {// println(it == "1") println(it) } val ary2 = arrayOfNulls<Long>(2) //每個數(shù)組元素中 填充一個null值 for (i in ary2.indices) {//indices 返回一個 索引范圍 : IntRange ary2[i] = 3L + Random().nextInt(10) println("ary2[$i] : ${ary2[i]}") //[] 可以用于 get 和 set 操作 } val ary3 = doubleArrayOf(1.0, 2.2, 3.3) //基本數(shù)據(jù)類型都對應(yīng)有一個 xxArrayOf函數(shù)}Import
package com.stone.basic.importsimport kotlin.*//import static java.lang.Math //Kotlin 不支持/*默認(rèn)import Kotlin file :— kotlin.*— kotlin.annotation.*— kotlin.collections.*— kotlin.comparisons.* (since 1.1)— kotlin.io.*— kotlin.ranges.*— kotlin.sequences.*— kotlin.text.*還有 Kotlin— JVM:— java.lang.*— kotlin.jvm.*//Kotlin 不支持 靜態(tài)方法導(dǎo)入 */
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選