&和和&&在Java? [英] Difference between & and && in Java?
问题描述
可能重复:
有什么区别|和||在Java?
差异在&和&&
我只是想知道& 和&& 是?
几天我写了的条件,如果
声明看起来像:
I was just wondering what the difference between & and && is?
A few days I wrote a condition for an if
statement the looked something like:
if(x < 50 && x > 0)
但是,我将&& 改为& 强>并且没有显示任何错误。有什么区别?
However, I changed the && to just & and it showed no errors. What's the difference?
示例:我编译了这个简单的程序:
Example: I compiled this simple program:
package anddifferences;
public class Main {
public static void main(String[] args) {
int x = 25;
if(x < 50 && x > 0) {
System.out.println("OK");
}
if(x < 50 & x > 0) {
System.out.println("Yup");
}
}
}
打印OK并且对。那么,如果它们都有效,那么我使用哪一个呢?
It printed "OK" and "Yup". So does it matter which one I use if they both work?
推荐答案
&
是按位的。
&&
是合乎逻辑的。
&
is bitwise.
&&
is logical.
&
评估操作的两面。
&&
评估操作的左侧,如果它是 true
,它继续并评估右侧。
&
evaluates both sides of the operation.
&&
evaluates the left side of the operation, if it's true
, it continues and evaluates the right side.
这篇关于&和和&&在Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!