Kotlin基本类型

Quibbler 2020-6-7 1609

Kotlin基本类型

        Kotlin没有基础数据类型,但是封装了6种数值类型,分别是Double、Float、Long、Int、Short、Byte。此外还有字符类型Char。



1、Number

        Kotlin中定义的6中数值类型都继承自Number父类。父类定义如下,定义了7中方法,

	public abstract class Number public constructor() {
	    public abstract fun toByte(): kotlin.Byte
	    public abstract fun toChar(): kotlin.Char
	    public abstract fun toDouble(): kotlin.Double
	    public abstract fun toFloat(): kotlin.Float
	    public abstract fun toInt(): kotlin.Int
	    public abstract fun toLong(): kotlin.Long
	    public abstract fun toShort(): kotlin.Short
	}

        也就是Kotlin中所有的数值类型都可以调用上面的方法转换。



2、常量

        在《Kotlin基本语法》中介绍了变量,现在了解一下什么是常量。常量是不可变的字面值。

例:

    //Int类型数值常量
    val num: Int = 0x1000
    
    //Boolean
    val are: Boolean = false
    
    //Long类型常量。结尾大写L 或 不加L
    val time: Long = 100L



3、类型转换

        前面说到数值类型继承自Number,提供了7种方法相互之间转换。

	public fun toByte()
	public fun toChar()
	public fun toDouble()
	public fun toFloat()
	public fun toInt()
	public fun toLong()
	public fun toShort()

例:

    //Int转换成Byte
    var big: Int = 10000
    var b: Byte = big.toByte()

        而且也不能像Java那样,高精度的赋值给低精度自动截取,都是需要手动转换的,否则报错。比如将Long赋给Int不行的,将Int赋给Long是更不行。



4、相等比较 

        一个等号=是赋值的意识,比较用的是==或者===


4.1、比较值:==

        两个等号 == 用来比较变量的是否相等。

例:

    var a: Int? = 20000
    var b: Int? = 20000
    println(a == b)


4.2、比较地址:===

        使用三个等号:=== 比较对象的地址是否指向同一个位置。

例:自动装箱,相同的值但是地址不同,结果为false

    var b: Int? = 20000
    var c: Int? = 20000
    println(b === c)

        但是如果不自动装箱,输出的结果则为true,指向相同的地址。猜测是在常量池中。

    var b: Int = 20000
    var c: Int = 20000
    println(b === c)

        需要可空引用时,即类型?,数字、字符会被装箱。



5、Char字符

        单引号 括起来的字符,比如'0'、'z'。


5.1、Char->Int

        Char字符类型不能和Int类型直接操作(比如加减),这一点和Java有区别。可以转成Int

    var c: Char = '0'
    var num = c.toInt()
    //值为48


5.2、Int->Char

        Int值专为Char字符,直接调用toChar()即可。具体内部如何转换不得而知,但是似乎不会抛出异常,及时Int值多大。

    var num = 9999999
    num.toChar()
    //值为'陿'



6、Boolean

        计算机最基本的类型了,就是true和fale。空的Boolean使用Boolean?装箱即可。

        常用的Boolean运算符:&&(与)、||(或)、(非)



7、Array数组

        Kotlin中数组类型为Array。除了基本的Array,还有ByteArray, ShortArray, IntArray,用来表示各个类型的数组,省去了装箱操作,因此效率更高。


7.1、生成数组

        ①使用arrayOf函数

    //使用Array指明变量为数组类型时,必须指明数组模板类型。这里数组存储的为Int类型
    var array: Array<Int> = arrayOf(1, 1, 2)
    
    //自动类型推断
    var array1 = arrayOf(1, 1, 2)

        ②使用Array构造方法,Array构造方法定义如下:

    /**
     * Creates a new array with the specified [size], where each element is calculated by calling the specified
     * [init] function.
     *
     * The function [init] is called for each array element sequentially starting from the first one.
     * It should return the value for an array element given its index.
     */
    public inline constructor(size: Int, init: (Int) -> T)

例:

    var array: Array<Int> = Array(3, { i -> (i * 2) })
    //数组:[0,2,4]


7.2、数组元素

        使用下标运算符[]直接访问数组指定位置的元素,内部是调用get(index: Int): T方法实现的

    var array: Array<Int> = arrayOf(1, 1, 2)
    var v = array[2]

        而给数组中指定位置赋值,内部则又是调用set(index: Int, value: T)方法实现

    array[1] = 2



8、String字符串

        和Java中的字符串一样,多一个编辑特性,三引号 """ ,括起来的字符支持多行字符串。编辑器默认加上trimIndent()方法

    val text = """
        你好,Kotlin
    """.trimIndent()

        此外还有trimMargin()方法

 /**
  * 修剪源字符串的每一行中的前导空格字符,后跟[marginPrefix],
  * 如果它们是空白的,则删除第一行和最后一行(注意,空白与空白是不同的)。
  *
  * 如果除第一行和最后一行空白行外不包含[marginPrefix],则不会影响该行。
  *
  * 不保留原始的行尾。
  */
public fun String.trimMargin(marginPrefix: String = "|"): String =
    replaceIndentByMargin("", marginPrefix)



学习资料:

        Kotlin 基本数据类型

       Java常量:Java常量的定义和分类


不忘初心的阿甘
最新回复 (0)
    • AI笔记本-欢迎来到 AI 驱动博客时代 🚀
      2
        登录 注册 QQ
返回
仅供学习交流,切勿用于商业用途。如有错误欢迎指出:fluent0418@gmail.com