I know it's not possible to call the equals method on a null object like that:
//NOT WORKING
String s1 = null;
String s2 = null;
if(s1.equals(s2))
{
System.out.println("NOT WORKING :'(");
}
But in my case I want to compare two objects from two database and these two objects can have attributes null...
So what is the method to compare two attributes, knowing that we are not sure that the value is null or filled.
This method is good or not ?
//WORKING
String s1 = "test";
String s2 = "test";
if(s1 == s2 || s1.equals(s2))
{
System.out.println("WORKING :)");
}
//WORKING
String s1 = null;
String s2 = null;
if(s1 == s2 || s1.equals(s2))
{
System.out.println("WORKING :)");
}
I'm not sure because in this case it's not working ... :
//NOT WORKING
String s1 = null;
String s2 = null;
if(s1.equals(s2)|| s1 == s2 )
{
System.out.println("NOT WORKING :'''(");
}
7. == and != The comparison and not equal to operators are allowed with null in Java. This can made useful in checking of null with objects in java.
Code Correctness: null Argument to equals()equals(null) will always be false. The program uses the equals() method to compare an object with null . This comparison will always return false, since the object is not null . (If the object is null , the program will throw a NullPointerException ).
No , null is not an object.It is a reference type and its value does not refer to any object and so there is no representation of null in memory.
out. println("(Object)string == number: " + ((Object)string == number)); To conclude this post and answer the titular question Does null equal null in Java? the answer is a simple yes.
I generally use a static utility function that I wrote called equalsWithNulls
to solve this issue:
class MyUtils {
public static final boolean equalsWithNulls(Object a, Object b) {
if (a==b) return true;
if ((a==null)||(b==null)) return false;
return a.equals(b);
}
}
Usage:
if (MyUtils.equalsWithNulls(s1,s2)) {
// do stuff
}
Advantages of this approach:
You will need to check atleast one is not null before doing equals method -
if(s1 == s2 || (s1!=null && s1.equals(s2))) {
System.out.println("WORKING :)");
}
here s1==s2
will work incase of null==null
. But if even one is not null, then you need to check atleast s1 before doing equals.
Update: As edited by @'bernard paulus', if you are using Java 7, you can use java.util.Objects.equals(Object, Object)
Another option to use:
Objects.equals(identification, criteria.getIdentification())
java.util.Objects
This class consists of static utility methods for operating on objects. These utilities include null-safe or null-tolerant methods for computing the hash code of an object, returning a string for an object, and comparing two objects.
Since: 1.7
public static boolean equals(Object a, Object b)
Returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned. Otherwise, equality is determined by using the equals method of the first argument.
Try using the ObjectUtils class from org.apache.commons.lang
public static boolean equals(java.lang.Object object1,
java.lang.Object object2)
From the api docs...
Compares two objects for equality, where either one or both objects may be null.
ObjectUtils.equals(null, null) = true
ObjectUtils.equals(null, "") = false
ObjectUtils.equals("", null) = false
ObjectUtils.equals("", "") = true
ObjectUtils.equals(Boolean.TRUE, null) = false
ObjectUtils.equals(Boolean.TRUE, "true") = false
ObjectUtils.equals(Boolean.TRUE, Boolean.TRUE) = true
ObjectUtils.equals(Boolean.TRUE, Boolean.FALSE) = false
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