I tried the following code :
if (!object.equals(null)) {
object.setItem(object1.getTime());
}
Here, object and object1 are objects of two classes.
The problem is that it enter into the statement for a null value.
I tried the following code :
if (!object.equals(null)) {
object.setItem(object1.getTime());
}
Here, object and object1 are objects of two classes.
The problem is that it enter into the statement for a null value.
For comparisons with null in Java, you use object != null
if(object != null){
//
}
The equals()
method in Java compares two objects equality according to the equals() method contract which you have to override or it follows the Object class equals() method contract.
In any case, equals() method compares two objects. null is not an object.
null is a reference type whose check should be done by using object != null
.