开发者

why String.class == "test".class

开发者 https://www.devze.com 2023-01-30 02:15 出处:网络
Anyone have any idea, why the ==sign works on .class comparison? Besides, the javadoc for getClass() method as

Anyone have any idea, why the == sign works on .class comparison?

Besides, the javadoc for getClass() method as

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.

The actual result type is Class<? extends |X|> where |X| is the erasure of the static type of the expression on which getClass() is开发者_JS百科 called. For example, no cast is required in this code fragment:

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

What does the "object locked by static synchronized methods" means?


== does identity comparison, and there's (normally) only one copy of each class. "object locked by static synchronized methods" means just what it says; it's the object that methods declared static and synchronized will attempt to lock.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号