Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Null safe equals before API v19

I have two objects Object1 and Object2, which may or may not be null. I want to test null safe equality between them (true if both null, or both equal) I have done my research and found Objects.equals(Object1,Object2), which fulfills its purpose.

But the problem is that it's only for API level 19+. How do I make this compatible with lesser APIs?

like image 270
Stephane Maarek Avatar asked Jun 02 '26 23:06

Stephane Maarek


1 Answers

There are several options.

  1. You could override the equals() method in the class you are comparing, and use obj1.equals(obj2)

  2. You could use the hashCode() method of Object to determine if they are equivalent, by using obj1.hashCode() == obj2.hashCode()

  3. Just using plain old == determines weather two variables contain the same instance of a class.

If these aren't null safe, you can easily write your own null checking with obj == null

This should be completely independent of the Android Api.

like image 188
wgoodall01 Avatar answered Jun 04 '26 13:06

wgoodall01