Kotlin中基本类型的定义及用法
val aBoolean: Boolean = true val anotherBoolean: Boolean = false val anInt: Int = 8 val anotherInt: Int = 0xFF //255 val moreInt: Int = 0b00000011 //3 val maxInt: Int = Int.MAX_VALUE //2147483647(2的31次方-1) val minInt: Int = Int.MIN_VALUE //-2147483648(-2的31次方) val aLong: Long = 12368172397127391 val another: Long = 123 val maxLong: Long = Long.MAX_VALUE //(2的63次方-1) val minLong: Long = Long.MIN_VALUE //(-2的63次方) val aFloat: Float = 2.0F //(2.0是双精度类型,必须要加上F) val anotherFloat: Float = 1E3f //(10的三次方) val maxFloat: Float = Float.MAX_VALUE val minFloat: Float = -Float.MAX_VALUE val aDouble: Double = 3.0 //感觉Double比Float要好 val anotherDouble: Double = 3.1415926 val maxDouble: Double= Double.MAX_VALUE val minDouble: Double= -Double.MAX_VALUE val aShort: Short = 127 val maxShort: Short = Short.MAX_VALUE //32767 val minShort: Short = Short.MIN_VALUE //-32768 val maxByte: Byte = Byte.MAX_VALUE //127 val minByte: Byte = Byte.MIN_VALUE //-128 val aChar: Char = '0' //字符用',字符串用" val bChar: Char = '中' val cChar: Char = '\u000f' //"\u"表示Unicode val string: String = "HelloWorld" val fromChars: String = String(charArrayOf('H', 'e','l','l','o','W','o','r','l','d')) val rawString: String = """ \t \n \\\\\\$$$ salary """ //长字符串无法使用转义符
Kotlin中基本类型转换
*Kotlin不支持隐式转换
val anInt:Int = 5
val aLong:Long = anInt.toLong()