Java 是否保证 Object.getClass() == Object.getClass()? [英] Does Java guarantee that Object.getClass() == Object.getClass()?

查看:26
本文介绍了Java 是否保证 Object.getClass() == Object.getClass()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里的意思是身份平等.

I really do mean identity-equality here.

例如,以下内容是否总是打印true?

For example, will the following always print true?

System.out.println("foo".getClass() == "fum".getClass());

推荐答案

是的,类标记是唯一的(对于任何给定的类加载器,即).

Yes, class tokens are unique (for any given classloader, that is).

即您将始终获得对在同一个类加载器领域中的同一个物理对象的引用.但是,不同的类加载器将加载不同的类标记,同时,当两个不同的类加载器加载相同的类定义时,会认为该类定义不同.

I.e. you will always get a reference to the same physical object within the same classloader realm. However, a different classloader will load a different class token, in conjunction with the fact that the same class definition is deemed different when loaded by two distinct classloaders.

请参阅之前的内容我的回答以演示这一点.

这篇关于Java 是否保证 Object.getClass() == Object.getClass()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆