87

Can we do a == on a Class variable instead of equals and expect the same result?

For example:

Class clazz = xyz;

Case A:

if(clazz == Date.class) {
// do something
}

Case B:

if(Date.class.equals(clazz)) {
// do something
}

Are Case A and Case B functionally same?

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
Ramesh
  • 3,841
  • 5
  • 22
  • 28
  • 2
    Possible duplicate -http://stackoverflow.com/questions/971954/difference-between-equals-and – mre Sep 06 '11 at 15:40
  • 15
    @mre, well my question is specifically about instances of type Class. Is the class loader guaranteed to return the same instance of Class for a given data type or not. – Ramesh Sep 06 '11 at 15:43
  • Based on some quick experiments, it looks like yes. I don't know if it's guarunteed, though. – Clockwork-Muse Sep 06 '11 at 15:48

3 Answers3

101

Class is final, so its equals() cannot be overridden. Its equals() method is inherited from Object which reads

public boolean equals(Object obj) {
    return (this == obj);
}

So yes, they are the same thing for a Class, or any type which doesn't override equals(Object)

To answer your second question, each ClassLoader can only load a class once and will always give you the same Class for a given fully qualified name.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 11
    is it possible that two different class loaders produced different classes and they came across? – voipp Jul 24 '15 at 08:20
  • 4
    @voipp it is possible that you can have two instances of the "same" class obtained from two different class loaders, but such a situation would not arise under normal circumstances – kbolino Oct 28 '15 at 03:02
  • 1
    And even if that situation did arrive, the two classes would still be considered distinct according to the equals method. You'd have to compare the classes by name if you wanted to know if they are the "same", but they could still be two different versions of the same class or even an accidental collision created by two different developers. From the jvm's perspective of course, they're not interchangeable, even if they do happen to be identical. – Jeremy Huiskamp Feb 17 '16 at 15:18
  • @JeremyHuiskamp You can have two classes with the same name in different class loaders, but if two classes have the same name and class loader then it is the same class. – Peter Lawrey Feb 17 '16 at 16:38
  • 3
    That's what I said, isn't it? I was referencing @kbolino's comment about obtaining two instances of the "same" class from two different class loaders. – Jeremy Huiskamp Feb 18 '16 at 13:39
  • 2
    @kbolino Once example of where multiple instances of the "same" class obtained from different class loaders is a bit more common is in the world of OSGI and ESBs (like ServiceMix). – Peter Kirby Jan 11 '17 at 17:08
  • 1
    I had the same question, and I found the Grepcode for java.lang.Class illuminating: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/lang/Class.java As we can see, equals comes from Object. :) – Haakon Løtveit Aug 11 '17 at 10:19
6

Yes.

Take a look at the Class class description and note that it inherits equals from Object, for which the method reads:

"The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true)."

cheeken
  • 33,663
  • 4
  • 35
  • 42
4

Yes, since the code for equals(...) for class is the following:

public boolean equals(Object obj) {
    return (this == obj);
}
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453