加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 为什么String.class ==“test”.class

发布时间:2020-12-15 08:36:57 所属栏目:Java 来源:网络整理
导读:任何人都有任何想法,为什么==符号适用于.class比较? 另外,getClass()方法的javadoc为 Returns the runtime class of this Object. The returned Class object is the object that is locked by static synchronized methods of the represented class. 实际
任何人都有任何想法,为什么==符号适用于.class比较?

另外,getClass()方法的javadoc为

Returns the runtime class of this
Object. The returned Class object is
the object that is locked by static
synchronized methods of the
represented class.

实际结果类型是Class<? extends | X |>其中| X |是擦除调用getClass()的表达式的静态类型.例如,此代码片段中不需要强制转换:

Number n = 0; 
Class<? extends Number> c = n.getClass();

“静态同步方法锁定的对象”是什么意思?

解决方法

==进行身份比较,并且(通常)每个类只有一个副本. “由静态同步方法锁定的对象”意味着它所说的内容;它是声明为static和synchronized的方法将尝试锁定的对象.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读