详解 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_000
val creditCardNumber = 1234_5678_9012_3456L
val socialSecurityNumber = 999_99_9999L
val hexBytes = 0xFF_EC_DE_5E
val bytes = 0b11010010_01101001_10010100_10010010
fun 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) – 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中 并不是一个数值类型
// println(r)
// }
if (r.toInt() == 118) {//可以用toInt() 来进行比较
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
支持使用\转义: \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>) {
/* 使用三个双引号开头与结尾, 中间可以包含 任何 非转义字符 */
val text = """
|Tell me and I forget.
|Teach me and I remember.
|Involve me and I learn.
|(Benjamin Franklin)
>admin""".trim().trimMargin().trimMargin(">") //trimMargin 去掉前缀,默认以|作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.types
import 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 创建数组
// val asc = Array(5, { i -> (i * i).toString() })
val asc = Array(5, { i -> Math.random() }) //使用构造函数创建数组:后面的lambda参数,表示设置每个index上的元素
for (it in asc) {
// println(it == "1")
println(it)
}
val ary2 = arrayOfNulls<Long>(2) //每个数组元素中 填充一个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) //基本数据类型都对应有一个 xxArrayOf函数
}
Import
package com.stone.basic.imports
import kotlin.*
//import static java.lang.Math //Kotlin 不支持
/*
默认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 不支持 静态方法导入
*/
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。