0

Is this bad practice or any performance hit, this is to check x is not equal to null

    if( !(x == null) ){
    System.out.println("x is not a null value");
    }
someone
  • 6,577
  • 7
  • 37
  • 60

6 Answers6

6

The normal way to do this is:

if(x != null){
    System.out.println("x is not a null value");
}

There's nothing wrong with checking if the value is not null!

John Farrelly
  • 7,289
  • 9
  • 42
  • 52
4

It is bad practice to do without making the reason to do so clear. It's not clear in your example why you are making the check. A common example might be something like

if (s == null || s.isEmpty()) // if s empty or not set.

or

if (s != null && s.length() > 0)

Usually, you do this when you need it in which case performance isn't important.

Normally you would write

if(x != null)

or better

if (x == null) {
   // handle null, assuming this is not empty.
} else {
   // handle not null
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 2
    Why should the `x == null` style be better? Especially when you would end up with an empty then block like in the example given. – Henry Jan 01 '13 at 11:15
  • 3
    What I want to discourage is that the idea that if a value is `null` you can pretend you can ignore it. It is better to start with the assumption you need to handle it and drop it if you don't. – Peter Lawrey Jan 01 '13 at 11:31
  • handling it is the purpose of the function. so imho it should happen not in a branch. – Johannes Schaub - litb Jan 01 '13 at 11:35
1

Performance-wise it is unlikely to be relevant, because you can trust the compiler to optimize that.

It's just a question of style. Style is always subjective, but I would say if (x != null) is more concise and more readable.

Philipp
  • 67,764
  • 9
  • 118
  • 153
1
if(x != null) is recommended and easy to read "X is not equal to null"

if(!(x == null)) can't be read as "X is not equal to null"

Android Killer
  • 18,174
  • 13
  • 67
  • 90
1

Just to add here best practice is to do

if(null != x) {
System.out.println("x is not null");
}

instead of
if(x != null) {
System.out.println("x is not null");
}

I know in java anyways will work but this will save you in other languages like c++ where you might accidently assign null to x for example,

if(x = null) {
printf("x is not null");
}

Prateek
  • 523
  • 2
  • 13
  • The best practice in Java is to put the thing that you are comparing, the thing that could be one of a number of values, first, just like you would do in the English language. In languages like C++, there is an overriding concern where, because there is no boolean type, putting `null` first avoids a certain class of typing mistakes. But it makes code less readable because it goes against the order of natural language. – Erwin Bolwidt Jan 13 '16 at 08:48
0
if( !(x == null) ){
System.out.println("x is not a null value");
}

well if condition returns a boolean true of false. so, writing above will not effect anything. according, to above code the condition you wrote is "if is not true" then do something! and as others have suggested to write code as if(x != null) is better way and less confusing ;)

Muhammad Talha Akbar
  • 9,952
  • 6
  • 38
  • 62