• Post Reply Bookmark Topic Watch Topic
  • New Topic

Check for nulls using "&&"  RSS feed

 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI All,
I am checking for nulls in my program. IF values are not pointing to null, only then, code with in IF conditoin should execute. When I was trying to unit test with null values,
strangly, code inside IF is getting executed. Please let me know where i am going wrong.


Please help
 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anu satya wrote:HI All,
I am checking for nulls in my program. IF values are not pointing to null, only then, code with in IF conditoin should execute. When I was trying to unit test with null values,
strangly, code inside IF is getting executed. Please let me know where i am going wrong.


Please help


I see a similar thread
http://www.coderanch.com/t/560736/java/java/Return-Statement-if-condition

Anyways, when should the code within if execute
1. neither first nor second variable is null
2. either one can be null but not both


 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Trivedi,
Yes, That is also my post.

In this new post, IF condition should be executed ONLY when both the values are not null.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it is the case of

Not A AND NOT B


http://en.wikipedia.org/wiki/De_Morgan%27s_laws


NOT (A OR B) = (NOT A) AND (NOT B)

Can you correct your if condition accordingly?

 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akhilesh Trivedi wrote:So it is the case of


NOT (A OR B) = (NOT A) AND (NOT B)

Can you correct your if condition accordingly?


Thans for the reply.
I have written the code like:

Question:
1. IS the above logic correct?
2. Should i use & instead of &&?

Please reply.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
& is a bitwise operator and only && should be used here - Operators summary

Note that if a is null, then the other condition of null check of b will not be executed - Read Conditional Operators
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all. My problem is fixed.

 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:& is a bitwise operator and only && should be used here - Operators summary

Note that if a is null, then the other condition of null check of b will not be executed - Read Conditional Operators


& is not only bit-wise operator but also holds for being logical operator
where as && is short-circuit logical operator.

if we have two conditions, A and B then
in


both the condition will be evaluated.

Where as in



if the first condition is false, second "B" will not be evaluated, because if A is false then , regardless of B, "A AND B" will also be false.

There is similar thing for use of | and || operator. In,



if A is true then B will not be evaluated. because if A is true then regardless of B, A OR B will also be true.

However in


both A & B will be evaluated.

Anu satya wrote:Thanks all. My problem is fixed.



Thanks for letting us know. Whenever you post a thread and feel it is fixed/resolved you can reply the same and can also click "Resolved" button at the very bottom of the thread.
I think you should be able to see "Resolved" button (at bottom somewhere near "Post Reply") ONLY for the thread that YOU have started.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akhilesh Trivedi wrote:
& is not only bit-wise operator but also holds for being logical operator where as && is short-circuit logical operator.

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