Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why String.class == "test".class

Tags:

java

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?

like image 585
engineer Avatar asked May 20 '26 01:05

engineer


1 Answers

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

like image 144
Ignacio Vazquez-Abrams Avatar answered May 21 '26 15:05

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!