2

How do we compare null values ? For example, I am trying to compare null by

class_name == "null"

But the code doesn't checks if the class is null. What's wrong ?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199

3 Answers3

6

You are comparing it to the reference of the String "null".

Remove the double quotes so you get the special null type (JLS).

You want:

class_name == null
              ↑
Maroun
  • 94,125
  • 30
  • 188
  • 241
1

class_name == null because in your way you compare it to string not to null

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Tareq Salah
  • 3,720
  • 4
  • 34
  • 48
  • 2
    is null an object or a data type, in Java ? – user3127386 Dec 22 '13 at 15:51
  • I'm sorry null is not an object in java (It is object in JavaScript) null is special type in java if you want more information about it in java you can look at this question http://stackoverflow.com/questions/2707322/what-is-null-in-java – Tareq Salah Dec 22 '13 at 15:53
0
if(class_name == null)
    // do something;
gunr2171
  • 16,104
  • 25
  • 61
  • 88