Jackie
Jackie

Reputation: 25979

why String.class == "test".class

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 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?

Upvotes: 3

Views: 238

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799062

== 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.

Upvotes: 7

Related Questions