Overwritten equals() method requires null check? (java) [duplicate] - java

This question already has answers here:
Is null check needed before calling instanceof?
(8 answers)
Closed 4 years ago.
I have written a simple equals() method for a class:
#Override
public boolean equals(Object obj) {
if(obj instanceof Cl) {
Cl u = (Cl)obj;
return u.i == i;
} else {
return false;
}
}
As I know, if I want compare if the class object equals to a null object, this returns false because of the instanceof, but according to my uni teacher this is a bad implementation, as the null check is missing. Can someone confirm for me if my theory is correct or not?

I think, a null check is not required and should not be there, because in this case there will not be any compile-time error or any exception if obj is null, because of the check if (obj instaceof C1).

Related

What is the difference between overriden equals and equals? [duplicate]

This question already has answers here:
What is the difference between == and equals() in Java?
(26 answers)
Closed 3 years ago.
I am new to Java and StackOverflow but as i have read some of the answers about equals they stated :
Equals method compares two objects for their identity and if they are
identical it returns TRUE . whereas if you don't override the method Equals
it acts like ==(which returns true if 2 variables refer to the same object).
Integer x = new Integer(4);
Integer y = new Integer(4);
System.out.println(x.equals(y));
System.out.println(x == y);
If the queries above are correct why does this code print TRUE and FALSE since we are not overriding the method equals?
Because class Integer does override the equals method and it's implementation is the following:
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}

What does the this == obj syntax mean? [duplicate]

This question already has answers here:
What is the meaning of "this" in Java?
(22 answers)
What is the difference between == and equals() in Java?
(26 answers)
Closed 4 years ago.
the following is the equals method from the String class:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String aString = (String)anObject;
if (coder() == aString.coder()) {
return isLatin1() ? StringLatin1.equals(value,aString.value)
: StringUTF16.equals(value, aString.value);
}
}
return false;
}
What does the comparison: 'this == anObject' at the first if statement mean?
It compares the memory addresses for both the object passed as parameter and the one you call equals from. If they are in the same memory address, they are obviously the same object.
Otherwise, it keeps checking for other ways to compare if they are actually equivalent objects.

Check if object is of type String [duplicate]

This question already has answers here:
How to determine an object's class?
(13 answers)
Closed 6 years ago.
I need to write a new method that checks if certain values are of the type String or not.
I have two objects and I want to be able to check if these two objects are strings, if they are to then return true, and false otherwise.
I did start off with the following method:
public boolean stringTest()
{
boolean aString;
}
But could not get anything to work after that, any help would be greatly appreciated!
You can make use of instanceof and rewrite your method
public boolean stringTest(Object any)
{
return any instanceof String;
}
then
stringTest(townName); // true
stringTest(new Integer()); // false
Use instanceof:
public boolean isString(Object o) {
return o instanceof String;
}

What is the difference between equales and == [duplicate]

This question already has answers here:
what is the difference between == operator and equals()? (with hashcode() ???)
(6 answers)
Closed 8 years ago.
I am new to java, and i was wondering what is the difference between equales and ==,
i know you can over ride equales, and you can't overrdie ==, but how?
Lets say i have java class Dogs, and if dogs are from same kind i want them to be equale, how can i do it?
public class Dogs{
private String dogKind;
public Dogs(String kind){
this.dogKind = kind
}
public String getDogKind(){
return this.dogKind;
}
}
So where in how i override equales?
Just simply read online(So many good answers), and if you dont understand:
You need to add this in the Dogs class, simply do:
#Override
public boolean equals(Object obj) {
if (!obj instanceof Dogs){
return false;
}
return this.dogKind.equals(((Dogs)obj).getDogKind());
}
The first part makes sure that the object given to the method is not null or from a different class.
The second part simply uses String equals, to check if the Strings are the same or not.

Trouble in overriding the equals(...) method in Java [duplicate]

This question already has answers here:
When overriding equals in Java, why does it not work to use a parameter other than Object?
(7 answers)
Closed 9 years ago.
I have a superclass Order which has a subclass OrderBook. The equals(...) method is overridden in the OrderBook. The method implementation is as follows:-
public boolean equals(Order o)
{
if(o==null){
System.out.println("object is null.");
return false;
}
if(o==this){
System.out.println("The object is itself.");
return true;
}
if(o instanceof OrderBook)
{
OrderBook o1 = (OrderBook)o;
if(!(o1.productId.equals(productId))){
System.out.println("productId mismatch.");
return false;
}
if(!(o1.customerId.equals(customerId))){
System.out.println("customerId mismatch.");
return false;
}
if(o1.book!=book){
System.out.println("book mismatch.");
return false;
}
}
return true;
}
I am encountering an unexpected output if I give the following statement:-
Order order1 = new OrderBook("Algorithms","Kunal",false);
Order order2 = new OrderBook("Algorithms","Kunal",false);
System.out.println(order1.equals(order2));
It should display true but its displaying false. Also, if I change the parameter in the equals(...) to Object o, then the whole program runs without any problem.
My doubt is that why are we using Object when we should be using Order?
public boolean equals(Order o)
Should be
public boolean equals(Object o)
Explanation
This is because you're attempting to override a method. When you override a method, you need to match the signature exactly. The signature is made up of:
The name of the method
The type of the arguments
The number of arguments
The return type.
The type of the argument in the original method signature was Object, yet you've provided an object of type Order. Hence, the runtime will treat these are two distinct methods.
try putting this instead of your block in the books comparing part
if(!o1.book.equals(book)){
System.out.println("book mismatch.");
return false;
}
And change the signature it should be equals(Object o)
Also do not forget the #Override annotation
To override a method in Java, you must match its signature exactly. The signature of Object#equals is:
public boolean equals(Object o)
a fairly standard approach is to overriding equals is:
public boolean equals(Object o) {
if (obj == null)
return false;
if (obj == this)
return true;
if (!(obj instanceof Order))
return false;
// specific comparisons for your Order object
}
Everyone did a good job explaning how to make it work. I want to explain why your's didnt work: (pay close attention to the type of parameter in equals())
Object class has equals(Object ojb). When JVM encounters the statement "order1.equals(order2)" it says to itself: Hmmmm ... order is of type Order and references OrderBook. I need to use the overriden equals() method if OrderBook has one. Dang!! OrderBook doesnt have equals(Object) instead it has equals(Order). So Im going to use equals(Object) from Object class.
What you have is called OVERLOADING. not overriding.

Categories

Resources