• Post Reply Bookmark Topic Watch Topic
  • New Topic

if statements  RSS feed

 
Ananth Ram
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

I want to know on the difference between the below and which one is appropriate.

if(!(oType != null)) {
// Code
}

or the other one

if(oType == null) {
// code
}

Thanks,
Ram
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i find the double '!' in the first example confusing, and the second very clear.
 
Ananth Ram
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Fred
 
Devaka Cooray
Marshal
Posts: 5649
765
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ananth Ram wrote:I want to know on the difference between the below and which one is appropriate.

There is actually no difference (not not equals implies 'equals').

Ananth Ram wrote:which one is appropriate.

The second one is appropriate as Fred said.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can always work out the logic on paper:



you can see that in the end, you get the same results (true for null, false for notNull). I always find the best way to figure this stuff out is to break it down step by step, and add in each component.
 
Ananth Ram
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you all for the insights.

Also just curious to know on the below.

if(null != oType)

and

if(oType != null)

What is the advantage if there are any??

Thanks
Ram
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ananth Ram wrote:
Thank you all for the insights.

Also just curious to know on the below.

if(null != oType)

and

if(oType != null)

What is the advantage if there are any??


I dont find any difference . to some of the programmer if(null != oType) this habit comes from C . in C if(x=0) is a valid statement [execution will fail] . but in java it is not valid.
so, no need to worry
 
Christiaan Lombard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There would only be a difference when comparing two instances of wrapper classes like Interger, Double, etc.

when using == to compare two variables of these classes they are compared by value, where if you used != they will be compared by reference.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!