Comparison of Java objects [duplicate] - java

This question already has answers here:
Parentheses around data type?
(5 answers)
Closed 7 years ago.
I'm new to Java, and am reading a Java book; at one point it talks about when you may want to override the built in function, equals(). For instance, if an object has a variable ID, and two objects have the same ID, you may want them to be considered to be equal. It gave example code which looks more or less like:
public boolean equals(Object obj) {
if((obj != null) && (obj instanceof myClass)) {
myClass object1 = (myClass)obj;
if(this.ID == object1.ID) {
return true;
}
}
return false;
}
I don't fully understand what's going on in the third line. I'm unsure about why it's necessary and you can't just compare obj.ID and this.ID in the if() statement. My guess is that it's because obj is just declared as a generic object which may not have an ID, so you need to create a new object, object1 which is of the correct class to look at the ID.
Am I correct here? What exactly is going on in that line?

In your code Object obj is a reference to an Object. The code at this point makes no assumptions about which type of Object it is.
When you do
myClass object1 = (myClass) obj;
you are casting the type of the reference to an object which this will either succeed because it is an instance of that type or fail throwing ClassCastException.
This creates a new reference but the underlying object is unchanged by this, nor is it copied.
Note: the obj != null check is redundant as null is not an instanceof of any class, nor does it trigger an exception. i.e.
Object o = null;
boolean b = o instanceof Object; // always false, never throws an exception.
A shorter version of this method could read.
public boolean equals(Object obj) {
if(obj instanceof myClass) {
myClass object1 = (myClass)obj;
return ID == object1.ID;
}
return false;
}
or even
public boolean equals(Object obj) {
return obj instanceof myClass && ID == ((myClass) obj).ID;
}
In Java 8 you could write
public boolean equals(Object obj) {
return Optional.ofNullable(obj)
.filter(o - > o instanceof myClass)
.map(o -> (myClass) o)
.filter(m -> ID == m.ID)
.isPresent();
}

On the 3rd line, no object is being created.
The first thing you need to understand about java is that there are primitives like int, char, double and objects, which are everything else, and objects are always accessed by reference.
So, Object obj is a reference to objects of type Object, and at runtime it will be referring to some object.
Then, further down, when you say myClass object1 you are not creating any object; you are just declaring a variable called object1 which will be referring to objects of type myClass. But there is no object yet.
So, when you say myClass object1 = (myClass)obj; you are assigning the reference obj to the reference object1. And since it would normally be invalid to make an assignment between different types, you are using a type cast (the (myClass) part) to tell the compiler that you know what you are doing, and you are sure that obj will be pointing to an object of type myClass at that point. So, the compiler allows you to make the assignment.
After the assignment, both obj and object1 are pointing to the same object, but the usefulness of object1 now is that you can view this object as an object of type myClass, so you can access its members.

Your guess is almost correct: obj is declared to have type Object and Object can be anything, a String for example, it does not have to have the member named ID, so, you can't just look at it to compare. So, the code you quoted first check if the obj is of the same type (if it isn't, then you know it does not equal), and then (on the line you are asking about) casts it to that type.
I said, your guess was almost correct, because you suggested that a new object of type myClass is created. This is not true. The assignment myClass object1 = (myClass)obj; does not create any new objects, it merely makes a new variable object1 refer to to the same object referred by obj, and tells the compiler that it should now know that that object is actually of type myClass.

Yes. The third line is inside an if statement that says obj instanceof myClass. At that point you know that it is of type myClass. Assuming myClass has an ID like in your code then you know that both objects have ID properties and you can use those for comparison.

Research the term "Object Casting".
the obj instanceof myClass is making sure that obj is the same type as this. Now we know it is safe to cast and Object obj into a myClass object1

The test for null is not needed, because instanceof is false for nulls.
Just use:
if (obj instanceof myClass) {
Otherwise, your code is fine, assuming ID is a primitive (especially, not a String).

Related

String Literal and Object Literal comparison

I come up with the scenario & can't understand the logic behind it ? I understood the first one only, rest two unable to understand ? Please explain how internally it work?
public class Test {
public static void main(String[] args) {
String s = "Karan";
Object o = "Karan";
System.out.println(s.equals(o)); // True
System.out.println(o.equals(s)); // True
System.out.println(s == o); // True
}
}
String s = "Karan" this is a String object referred by a String reference.
Object o = "Karan" this is a String object referred by an Object reference. Super class can refer to object of sub-class.
For both s.equals(o) and o.equals(s) the underlying objects equal() is called, since the objects are of type String (even if reference 'o' is of type Object) String's equals is called which compares the value of the string which are equal. That is what you got true as result.
s == o compares the object reference of s and o. They were created using double quote which uses the String pool, creates the object once and refers it again. So both s and o are exactly same String object. This makes == return true.
There is a difference between the static type of an object and the dynamic type of a variable.
The static type is the type known at compile time. For Object o = "Karan";, the static type is the one at the left of o, i.e. Object. Now, one could ask why that is the case since at compile-time, it can be inferred that o is a String. Well if we consider something like
Object o;
if (someCondition) {
o = "Karam";
} else {
o = new StringBuffer();
}
then we cannot know whether o is a String or a StringBuffer, thus its static type is Object (since o is defined as Object).
The dynamic type is the type at runtime. In the example above, after the if-statement has been executed, o is either a String or a StringBuffer, but not both and nothing else. In your example, the dynamic type of Object o = "Karan"; is String.
When variables are compared through equals(...), they must have the same dynamic type in order for the comparison to return true (notice that this property is necessary, but not sufficient). Since o in your example is, in fact, a String AND the content is equal to s, s.equals(o) == o.equals(s) == true.
As to why s == o returns true: the question is answered in this post.

this in equals method

I am looking at the equals method, and I see this and I don't understand what it means...I do understand them when I see it in constructors and some methods but its not clear to me when they are in equals method something like this:
(obj == this) ...what does this mean here ? where does it come from ?
I understand when it says something like this.name = name;
from a method like this
#Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
sorry it might be a duplicate but I couldnt find anything...
this is the current Object instance. Whenever you have a non-static method, it can only be called on an instance of your object.
You are comparing two objects for equality. The snippet:
if (obj == this) {
return true;
}
is a quick test that can be read
"If the object I'm comparing myself to is me, return true"
. You usually see this happen in equals methods so they can exit early and avoid other costly comparisons.
You have to look how this is called:
someObject.equals(someOtherObj);
This invokes the equals method on the instance of someObject. Now, inside that method:
public boolean equals(Object obj) {
if (obj == this) { //is someObject equal to obj, which in this case is someOtherObj?
return true;//If so, these are the same objects, and return true
}
You can see that this is referring to the instance of the object that equals is called on. Note that equals() is non-static, and so must be called only on objects that have been instantiated.
Note that == is only checking to see if there is referential equality; that is, the reference of this and obj are pointing to the same place in memory. Such references are naturally equal:
Object a = new Object();
Object b = a; //sets the reference to b to point to the same place as a
Object c = a; //same with c
b.equals(c);//true, because everything is pointing to the same place
Further note that equals() is generally used to also determine value equality. Thus, even if the object references are pointing to different places, it will check the internals to determine if those objects are the same:
FancyNumber a = new FancyNumber(2);//Internally, I set a field to 2
FancyNumber b = new FancyNumber(2);//Internally, I set a field to 2
a.equals(b);//true, because we define two FancyNumber objects to be equal if their internal field is set to the same thing.
this refers to the current instance of the class (object) your equals-method belongs to. When you test this against an object, the testing method (which is equals(Object obj) in your case) will check wether or not the object is equal to the current instance (referred to as this).
An example:
Object obj = this;
this.equals(obj); //true
Object obj = this;
new Object().equals(obj); //false

How do I check if a given object is an instance of certain class when in Object[] array?

I was trying to find out how to determine which class a given object instantiate when it's in Object[] array. For example:
Object[] array = new Object[]{175, "sss", new Table(), true};
Object obj = array[0]; // hmm... can this be used as an integer or maybe as a string?
Is it even possible?
You can call getClass() to find out the class of a particular object, or you can use instanceof to check a specific type:
if (array[0] instanceof Integer) {
}
Normally having to do a lot of this indicates a weakness in your design though - you should try to avoid needing to do this.
You can try using instanceof or you can try getClass().isAssignableFrom(), whatever fits your needs
You can test whether it is an instance of a pre-known class (and cast it) like this:
if (obj instanceof String) {
String s = (String) obj; //casts the obj now you know it's a String
}
I like to think of this not as making any changes to the object but just as revealing its true character. For example, it's a bit like seeing a person and not knowing what language they speak - the person is still French, or Italian, just that you don't know which yet. The cast (i.e. (String) obj) is you telling the compiler the equivalent of "I know this person speaks French"
Or you can gets its class like this:
Class<?> clazz = obj.getClass();
A Class instance can be used to make the same check:
String.class.isInstance(obj) {
String s = String.class.cast(obj);
}

Is it better to use class.isEnum() or instanceof Enum?

I have an object. I want to check to see if it is of type enum. There are two ways to do this.
object.getClass().isEnum()
or
object instanceof Enum
Is one better?
In my opinion object instanceof Enum is better for several reasons:
It is very obvious what is asked here: "is this an enum"?
It doesn't risk a NullPointerException (if object is null, it will just evaluate to false)
It's shorter.
The only reason I'd see for using isEnum() would be if I only have access to the Class object and not to a concrete instance.
You need to use the latter (object instanceof Enum) because the former may not work with enum constants with constant-specific class bodies.
For example, for this enum type:
enum MyEnum {
FOO { }
}
The expression MyEnum.FOO.getClass().isEnum() returns false.
If you want to check if an object is a enum constant without instanceof Enum, you have to use this (much more complicated) expression:
static boolean isEnum(Object obj) {
Class<?> cls = obj.getClass();
Class<?> superCls = cls.getSuperclass();
// Be careful, Object.class.getSuperclass() returns null
return cls.isEnum() || (superCls != null && superCls.isEnum());
}

Check if an object belongs to a class in Java [duplicate]

This question already has answers here:
How to determine an object's class?
(13 answers)
Closed 9 years ago.
Is there an easy way to verify that an object belongs to a given class? For example, I could do
if(a.getClass() = (new MyClass()).getClass())
{
//do something
}
but this requires instantiating a new object on the fly each time, only to discard it. Is there a better way to check that "a" belongs to the class "MyClass"?
The instanceof keyword, as described by the other answers, is usually what you would want.
Keep in mind that instanceof will return true for superclasses as well.
If you want to see if an object is a direct instance of a class, you could compare the class. You can get the class object of an instance via getClass(). And you can statically access a specific class via ClassName.class.
So for example:
if (a.getClass() == X.class) {
// do something
}
In the above example, the condition is true if a is an instance of X, but not if a is an instance of a subclass of X.
In comparison:
if (a instanceof X) {
// do something
}
In the instanceof example, the condition is true if a is an instance of X, or if a is an instance of a subclass of X.
Most of the time, instanceof is right.
If you ever need to do this dynamically, you can use the following:
boolean isInstance(Object object, Class<?> type) {
return type.isInstance(object);
}
You can get an instance of java.lang.Class by calling the instance method Object::getClass on any object (returns the Class which that object is an instance of), or you can use class literals (for example, String.class, List.class, int[].class). There are other ways as well, through the reflection API (which Class itself is the entry point for).
Use the instanceof operator:
if(a instanceof MyClass)
{
//do something
}
I agree with the use of instanceof already mentioned.
An additional benefit of using instanceof is that when used with a null reference instanceof of will return false, while a.getClass() would throw a NullPointerException.
Try operator instanceof.
The usual way would be:
if (a instanceof A)
However, there are cases when you can't do this, such as when A in a generic argument.
Due to Java's type erasure, the following won't compile:
<A> boolean someMethod(Object a) {
if (a instanceof A)
...
}
and the following won't work (and will produce an unchecked cast warning):
<A> void someMethod(Object a) {
try {
A casted = (A)a;
} catch (ClassCastException e) {
...
}
}
You can't cast to A at runtime, because at runtime, A is essentially Object.
The solutions to such cases is to use a Class instead of the generic argument:
void someMethod(Object a, Class<A> aClass) {
if (aClass.isInstance(a)) {
A casted = aClass.cast(a);
...
}
}
You can then call the method as:
someMethod(myInstance, MyClass.class);
someMethod(myInstance, OtherClass.class);

Categories