Does Java guarantee that Object.getClass() == Object.getClass()?

前端 未结 4 2138
礼貌的吻别
礼貌的吻别 2020-11-27 06:41

I really do mean identity-equality here.

For example, will the following always print true?

System.out.println(\"foo\".getClass() ==         


        
4条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 07:11

    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.

    See this earlier answer of mine for a demonstration of this.

提交回复
热议问题