Sponsored Links
Ad by Google
This is again a very common interview question to check the basic concepts of java. Object is super class of all the classes in java and equals() method is exist inside this class.
Well, The equals() method as it's name implies this method is used to check the equality of the objects and you know the equality is possible only with more than one objects. So the same way, equals() method of Object class is used to check whether "this object" is equal to "other object".
For example whether the soccer balls was used in FIFA 2014 World Cup are equal to the soccer balls was used in FIFA 2010 World Cup. Definitely answer will be based on the comparison of the specifications used in both the soccer balls example, what are the Circumference, Diameter, Weight, Loss of pressure etc. But if question is something like In which FIFA World cup maximum soccer ball was consumed? 2010 or 2014. Answer will be based on comparison of number of soccer balls was used in both the game.
Ok, We will answer of both the question in terms of java programming later.
Before that let see the default implementation of equals() method in java.lang.Object class
For example,
Now, below code will print true because both fifa2010 and fifa2014 are belong to same reference.
To check whether two objects are equal or not based on the information(contents), you must override the equals() method. Here are the rules of overriding equals() method copied from JSR.
Let's override the equals() method by following the above five points in FIFASoccer class
This is the answer of the above question, for second question we already seen that for primitive data types default implementation of equals() method will work perfect.
Note: You must override the hashCode() method, if you override equals() method. Here is why hashCode() method in java.
That's it.
References:
Reference 1
Well, The equals() method as it's name implies this method is used to check the equality of the objects and you know the equality is possible only with more than one objects. So the same way, equals() method of Object class is used to check whether "this object" is equal to "other object".
For example whether the soccer balls was used in FIFA 2014 World Cup are equal to the soccer balls was used in FIFA 2010 World Cup. Definitely answer will be based on the comparison of the specifications used in both the soccer balls example, what are the Circumference, Diameter, Weight, Loss of pressure etc. But if question is something like In which FIFA World cup maximum soccer ball was consumed? 2010 or 2014. Answer will be based on comparison of number of soccer balls was used in both the game.
Ok, We will answer of both the question in terms of java programming later.
Before that let see the default implementation of equals() method in java.lang.Object class
public boolean equals(Object obj) { return (this == obj); }The equals() method will compares the two object with the help of == operator and return true if both the objects are equal. The == operator works perfect for primitive data types but for object not. The equals() method compares the references of the objects, if both the object belong to same reference then it will return true otherwise return false.
For example,
public static void main(String[] args) { FIFASoccer fifa2010 = new FIFASoccer(); FIFASoccer fifa2014 = new FIFASoccer(); System.out.println(fifa2010.equals(fifa2014)); }The above will print false, because fifa2010 and fifa2014 are not belong to same reference although they looks same.
Now, below code will print true because both fifa2010 and fifa2014 are belong to same reference.
public static void main(String[] args) { FIFASoccer fifa2010 = new FIFASoccer(); FIFASoccer fifa2014 = fifa2010; System.out.println(fifa2010.equals(fifa2014)); }Conclusion: The default implementation of equals() method of java.lang.Object class is used to check the reference of two objects are equal or not.
To check whether two objects are equal or not based on the information(contents), you must override the equals() method. Here are the rules of overriding equals() method copied from JSR.
- Reflexive: For any not null reference value x, x.equals(x) should return true.
- Symmetric: For any not null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
- Transitive: For any not null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
- Consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
- For any not null reference value x, x.equals(null) should return false.
Let's override the equals() method by following the above five points in FIFASoccer class
package com.test; public class FIFASoccer { private int circumference; private int diameter; private double weight; private int lossOfPressure; public FIFASoccer() { } public FIFASoccer(int circumference, int diameter, double weight, int lossOfPressure) { super(); this.circumference = circumference; this.diameter = diameter; this.weight = weight; this.lossOfPressure = lossOfPressure; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + circumference; result = prime * result + diameter; result = prime * result + lossOfPressure; long temp; temp = Double.doubleToLongBits(weight); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; FIFASoccer other = (FIFASoccer) obj; if (circumference != other.circumference) return false; if (diameter != other.diameter) return false; if (lossOfPressure != other.lossOfPressure) return false; if (Double.doubleToLongBits(weight) != Double .doubleToLongBits(other.weight)) return false; return true; } public static void main(String[] args) { FIFASoccer fifa2010 = new FIFASoccer(69,1,440,10); FIFASoccer fifa2014 = new FIFASoccer(69,1,440,10); System.out.println(fifa2010.equals(fifa2014)); } }This will print true because here we are overriding the default implementation of equals() method, now the equality test is done based on the content of both the objects and both the object has same content.
This is the answer of the above question, for second question we already seen that for primitive data types default implementation of equals() method will work perfect.
Note: You must override the hashCode() method, if you override equals() method. Here is why hashCode() method in java.
That's it.
References:
Reference 1
Sponsored Links
0 comments:
Post a Comment