?
Solved

QUICKIE: Safe to check for null and valid value in same statement?

Posted on 2005-03-29
5
Medium Priority
?
189 Views
Last Modified: 2010-03-31
Dear Experts:

In Java, is it always safe to do this:

Integer myInt;

// .. do some things...

if ((myInt != null) & (myInt.intValue() != 999))
{
  // did I blow up?
}

Is the test for null ALWAYS done first, so there is no risk of NullPointerException?

Thanks,
BrianMc1958
0
Comment
Question by:BrianMc1958
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 500 total points
ID: 13652910
>> Is the test for null ALWAYS done first
Yes
>> so there is no risk of NullPointerException
No
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13652924
I guess you meant "&&" instead of "&"
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13652967
>>>> Is the test for null ALWAYS done first
>>Yes
I mean: the left most part is always done first  (if you put "myInt!=null" as 2nd part NPE's can occur)

>>>> so there is no risk of NullPointerException
>>No
That's because if myInt==null the first part is false.
In that case the 2nd part is no longer validated, since it's useless.
Because
    false && whatever is always false
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13653100
Java == "Lazy Evaluation", so zzynx is right.

http://mindprod.com/jgloss/lazy.html
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13659417
Thanks for accepting
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question