简体   繁体   English

Java布尔方法返回

[英]Java boolean method return

I have a boolean method in my Java program and I'm wondering why NetBeans is recommending making this change to my code: 我的Java程序中有一个布尔方法,我想知道为什么NetBeans建议对我的代码进行此更改:

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. 两者都正常工作,但我无法理解NetBeans建议的变更背后的逻辑。 Neither true or false is being returned. 无论是真还是假都没有归还。 Could someone explain to me the logic behind this? 有人可以向我解释这背后的逻辑吗? Thanks 谢谢

NetBeans is exactly correct. NetBeans完全正确。 The expression is a boolean. 表达式一个布尔值。 You can easily prove it by making the change and trying it. 您可以通过进行更改并尝试来轻松证明它。

What value does 0 != 0 evaluate to? 0 != 0评估为什么值? (Hint: it's false ). (提示:这是false )。 What about 1 != 0 ? 1 != 0怎么样? (Hint: it's true ). (提示:这是true )。

Is that not what your more verbose code says? 这不是你的更详细的代码所说的吗?

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()' 可以通过替换来减少,用“doIsPrime()”函数替换'isPrime!= 0'

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 并通过删除空块周围的if语句进一步减少

return doIsPrime();

Now undo the substitution 'doIsPrime()' back to 'isPrime != 0' 现在撤消替换'doIsPrime()'回到'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. 但是,我发现它更好地展示了if语句冗余的原因。

Neither true or false is being returned. 无论是真还是假都没有归还。

False. 假。 The result of the comparison isPrime != 0 , a boolean is being returned, either true or false . 比较的结果是isPrime != 0 ,返回一个boolean ,无论是true还是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 . 如果isPrime0 ,则返回false ,否则返回true The != operator will yield false if isPrime is 0 , and true if it isn't 0 . !=操作会产生false ,如果isPrime0 ,和true如果不是0

这意味着它返回谓词的结果isPrime != 0如果isPrime = 0,则谓词为false,因此返回false如果isPrime!= 0,则谓词为true,因此返回true

It's just to reduce code. 这只是为了减少代码。

The expression isPrime != 0 returns a boolean. 表达式isPrime != 0返回一个布尔值。 It is false if isPrime == 0 and true if isPrime != 0 . 如果isPrime == 0则为false,如果isPrime != 0则为true。 Thus you can save the if statement and some lines of code. 因此,您可以保存if语句和一些代码行。

Just to add to what's already said: 只是添加到已经说过的内容:

Though your solution and NetBeans recommended approach are exactly correct, one other approach would also be: 虽然您的解决方案和NetBeans建议的方法完全正确,但另一种方法也是:

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. 我不知道NetBeans会用这种方法建议什么,但我想它会提出与上面相同的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM