3

I have a boolean method in my Java program and I'm wondering why NetBeans is recommending making this change to my code:

return isPrime != 0;

What I wrote was:

if (isPrime == 0) {
            return false;

        }
        else{
            return true;
        }

Both work correctly but I cannot understand the logic behind the change NetBeans is suggesting. Neither true or false is being returned. Could someone explain to me the logic behind this? Thanks

1
  • and to be frank: There is nothing wrong about that. It's describing what you want to express. But imagine 10 conditions such as that in a row, than you have 70 lines of code instead of 1. As long as the conditions you are testing are simple comparsions you should stay with the short cut notation. You will get used to read them as quickly as the above example and even read them much quicker after a while. Commented May 11, 2015 at 21:06

6 Answers 6

4

NetBeans is exactly correct. The expression is a boolean. You can easily prove it by making the change and trying it.

What value does 0 != 0 evaluate to? (Hint: it's false). What about 1 != 0? (Hint: it's true).

Is that not what your more verbose code says?

Sign up to request clarification or add additional context in comments.

4 Comments

Doesn't it rather return a boolean?
Those are boolean expressions. What are you asking?
Thanks duffymo I'm getting used to Java right now and trying to understand the logic behind short cuts in coding.
I wouldn't say that an expression is a boolean. I'd say it evaluates to a boolean and that boolean is a data type. But I'm not a native speaker.
2

Instead of

if (isPrime == 0) {
    return false;
} else {
    return true;
}

try

return (isPrime != 0);

It is the same thing, I'll show you the refactoring path. Starting with

if (isPrime == 0) {
    return false;
} else {
    return true;
}

is the same as

if (isPrime != 0) {
    return true;
} else {
    return false;
}

which can be reduced by substitution, substituting 'isPrime != 0' with the function 'doIsPrime()'

private boolean doIsPrime() {
   return isPrime != 0;
}

substituting in

if (doIsPrime()) {
    // guaranteed to be the same as above!
    return doIsPrime();
} else {
    return doIsPrime();
}

which can have both blocks reduced (as duplicated code)

if (doIsPrime()) {
}
return doIsPrime();

And reduced further by removing the if statement around the empty block

return doIsPrime();

Now undo the substitution 'doIsPrime()' back to 'isPrime != 0'

return isPrime != 0;

There was no need to really do the substitution; but, I find it better shows off the reasoning the if statement is redundant.

1 Comment

Haha, beautiful explanation. Like it!
1

Neither true or false is being returned.

False. The result of the comparison isPrime != 0, a boolean is being returned, either true or false.

Could someone explain to me the logic behind this?

The first code is equivalent to the second code. If isPrime is 0, then return false, else return true. The != operator will yield false if isPrime is 0, and true if it isn't 0.

Comments

1

It means its returning the result of the predicat isPrime != 0 If isPrime = 0, then the predicat is false, therefore it return false If isPrime != 0, then the predicat is true, therefore returning true

Comments

0

It's just to reduce code.

The expression isPrime != 0 returns a boolean. It is false if isPrime == 0 and true if isPrime != 0. Thus you can save the if statement and some lines of code.

Comments

0

Just to add to what's already said:

Though your solution and NetBeans recommended approach are exactly correct, one other approach would also be:

if (isPrime == 0) {
    return false;
}

return true;

I don't know what NetBeans would suggest with this approach but I guess that it would propose the same recommendation as it did above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.