简体   繁体   English

这 ! 和 && 运算符 Java

[英]The ! and the && operators Java

Ok so, I was working on a program and I ran into a little problem/question.好的,我正在开发一个程序,但遇到了一个小问题/疑问。

Eg例如

int num1 = 3, int num2 =0;   
while (!num1 == 1 && num2 == 0)
    { 
      ...
    }

Does the !! apply to the whole statment in, or only to the part before && ?适用于整个语句中,还是仅适用于&&之前的部分?

The ! ! part actually only applies to the num1 integer (Which is surely not what you want), not even the num1 == 1 part.部分实际上仅适用于num1整数(这肯定不是您想要的),甚至不适用于num1 == 1部分。

So your expression is basically,所以你的表达基本上是,

if (((!num1) == 1) && (num2 == 0)) {

The ! ! is applied before the && according to the Java order of operations , so the !根据 Java 的操作顺序&&之前应用,所以! only applies to the part before the && .仅适用于&&之前的部分。

Use IDE such as Eclipse and set Compiler compliance levels to latest Java versions.使用 Eclipse 等 IDE 并将编译器合规性级别设置为最新的 Java 版本。 You will get errors to help you make the right decision.您将收到错误信息以帮助您做出正确的决定。

And if you want to read how precedence works, then follow the official Java reference link here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html如果您想了解优先级的工作原理,请点击此处的官方 Java 参考链接: https : //docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

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

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