Dennis
Dennis

Reputation: 1547

Android testing for null and condition

Ola,

Android/JAVA question, I need to test if a object is not NULL and if it's not NULL check a value, thus avoiding runtime error. As my vb.net background I'm used to;

if (not BackgroundWorker1 = nothing) andalso (backgroundworker.status = running) then

Is there a charming, 'one IF'/single line way to do this in JAVA?

Thanks!

Upvotes: 0

Views: 4295

Answers (3)

Dirk
Dirk

Reputation: 31063

The moral equivalent of andAlso in Basic is && in Java. Note, that there is no easy equivalent to plain and (i.e., the non-short-circuit version of the logical operator).

Upvotes: 0

Ken Wayne VanderLinde
Ken Wayne VanderLinde

Reputation: 19349

There's a pretty direct translation:

if (BackgroundWorker1 != null && Backgroundworker1.status == running)
{
    // "then" part here
}

Just like VB's andalso operator, Java also uses short-circuit evaluation with its logical operators.

Upvotes: 0

Gustav Barkefors
Gustav Barkefors

Reputation: 5086

Sure,

if ( backgroundWorker != null && backgroundWorker.status == running ) {

works in Java since the statements are evaluated from left to right.

Upvotes: 1

Related Questions