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?
The major difference between the == operator and . equals() method is that one is an operator, and the other is the method. Both these == operators and equals() are used to compare objects to mark equality.
Operators are generally used for primitive type comparisons and thus == is used for memory address comparison and equals() method is used for comparing objects. Show activity on this post. Both == and . equals() refers to the same object if you don't override .
The equals() method compares two strings, and returns true if the strings are equal, and false if not.
Difference between == and . Equals method in c# The Equality Operator ( ==) is the comparison operator and the Equals() method in C# is used to compare the content of a string. The Equals() method compares only content.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With