• Post Reply Bookmark Topic Watch Topic
  • New Topic

No error when null checked against null  RSS feed

 
Gerald Amalraj
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(null == null){}

its allowed in any java environment...

whats the meaning of this line of code as it doesn't have any logical meaning

Why it doesn't throw any compilation errors

Regards
Gerald.J
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are comparing two objects using a Comparator, you can safely invoke the compare method if
you have the a==b check as the first line of the code.

It is very important to be able to do identity comparison on two null values.

You know that object parameters can take null values as arguments.
 
Gerald Amalraj
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll rephrase the question as simple as possible

eclipse accepts a line of code like this

if(null == null){}

the above line of code doesn't mean anything and it cannot be used elsewhere and there will not be any scenario where null is checked against null. [when i say null I literally mean the exact reserved keyword null]

my question is

why compiler is designed in such a way that it accepts this illogical line of code

my suggestion: should the compiler be designed in such a way that the if condition should accept only one null

Regards
Gerald.J
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes you think that null == null is illogical or should be prohibited? Otherwise you could not write this sort of code:-
Remember that null is simply another reference, but one which points to nothing. If you can write if (0 == 0) ... why can't you do the same with null. It is not illogical, but a trivial and vacuous tautology.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gerald Amalraj wrote:the above line of code doesn't mean anything and it cannot be used elsewhere and there will not be any scenario where null is checked against null.

Sure there is, as Campbell already explained:
if (x == null)
where x is null, and
if (x == y)
where both x AND y are null.

The fact that specifically
if (null == null)
doesn't make much sense doesn't mean that the condition can't arise.

Winston
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can even write if (true) or if (false). Your IDE may give you a warning, but it will definitely compile.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:You can even write if (true) or if (false)...

Indeed
while(true)
is used quite a lot.

Winston
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Rob Spoor wrote:You can even write if (true) or if (false)...

Indeed
while(true)
is used quite a lot.

But while (true) is actually a useful construct - there are times when an infinite loop is needed.
Whereas I can't think of a case (but I'm sure someone will know of one) where if (true) couldn't have just been left out.
i.e.

can always be written as just
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:But while (true) is actually a useful construct...

I knew I'd get that.

But my point was, if
while(true)
is useful, what's to say that
if (true)
isn't?

The compiler isn't some sort of language expert; it's basically a token analyser. And if you sent it down different verification (as opposed to syntactical) paths depending on whether it's dealing with an if or a while (or any other 'verbs' the language has that can take boolean expressions), you could end up with a very big program.

Winston
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider this code:



Now you've probably forgotten this, but when the compiler processes that, it actually compiles the code as if the value of the constant were hard-coded wherever it's used. (It did this way back in Java 1.0 days for some efficiency reason which I don't quite remember and which would be considered totally bogus these days.)

So here we have a working example of "if (true)" which nobody can complain is a tautology.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!