关于Kotlin "===" 的特性

后续补充

 

code:

 

	var x1: Int? = 127
	var x2: Int? = 127
	println("x1 === x2 : ${x1 === x2}")

	var z1: Int? = 128
	var z2: Int? = 128
	println("z1 === z2 : ${z1 === z2}")
	
	
	// 输出
	x1 === x2 : true
	z1 === z2 : false

分析:

查看Kotlin编译后的字节码

Integer x1 = 127

Integer x2 = 127

我们知道在java中 该代码会做自动装/拆包操作,编译器默认调用的是 Integer.valueOf(127) 

查看Integer valueof 源码 

 

    /**
     * Returns an {@code Integer} instance representing the specified
     * {@code int} value.  If a new {@code Integer} instance is not
     * required, this method should generally be used in preference to
     * the constructor {@link #Integer(int)}, as this method is likely
     * to yield significantly better space and time performance by
     * caching frequently requested values.
     *
     * This method will always cache values in the range -128 to 127,
     * inclusive, and may cache other values outside of this range.
     *
     * @param  i an {@code int} value.
     * @return an {@code Integer} instance representing {@code i}.
     * @since  1.5
     */
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
IntegerCache.cache 缓存着 -128 - 127 的值

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Kotlin中,可以使用赋值操作符"="将一个变量赋值为null。这意味着该变量可以指向null,即可为null引用。例如,可以将一个可空类型的变量赋值为null,如下所示:val p: Person? = null。这样做是为了实现Kotlin的空安全特性,通过使用"?."操作符可以避免产生空指针异常。在使用if语句判断null的情况下,也可以将变量赋值为null,如下所示:val str = getString() if (str == null) { println("null") } else { val count = str?.count() println(count) }。需要注意的是,对于一般的非空类型变量,是不允许将其赋值为null的,否则会导致编译错误。例如,var a: String = "abc" a = null // 编译错误。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [Kotlin中的空判断](https://blog.csdn.net/abs625/article/details/110187235)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Kotlin(五)null操作](https://blog.csdn.net/niuyongzhi/article/details/126515015)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Kotlin-null的处理](https://blog.csdn.net/IO_Field/article/details/53470628)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值