Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

null comparison

Tags:

java

This might sound like a real dumb question but please bear with me :) so I have a if condition in my code like if ((msgBO.getEmpId().equals("") == false )) { // do something } My question is, if I make the above statement as msgBO.getEmpId().equals(null) == false would it make any difference or this way I am trying to compare two different things?

like image 661
t0mcat Avatar asked Apr 21 '26 10:04

t0mcat


1 Answers

Yes, there is a big difference between "" (the empty String) and null (no String at all).

Any Object reference can point to null. That represents 'no data' or 'nothing.' The empty string "" represents a String with no length.

An example of this is the following:

String one = null;
String two = "";
int oneLength = one.length();
int twoLength = two.length();

The first call to length will throw a NullPointerException. And the second will return 0.

like image 69
jjnguy Avatar answered Apr 23 '26 22:04

jjnguy