简体   繁体   English

Java If块具有多个语句

[英]Java If block with multiple statements

I have a question about IF clause in Java . 我对Java中的 IF子句有疑问。 I have and expression: 我有和表达:

if ((someObject != null & connectedToTheInternet) || operate) {
    // some action
}

Is my logic right: if someObject != null equals to true and connectedToTheInternet equals false then we have (someObject != null & connectedToTheInternet) equals false and then we have the following block: 我的逻辑是正确的:如果someObject != null等于true,connectedToTheInternet等于false,那么我们(someObject != null & connectedToTheInternet)等于false ,那么我们有以下代码块:

if (false || operate) {
   // some action
}

And if operate equals true then // some action will be triggered? 如果operate等于true,那么// some action将触发// some action

Just a first note: logical AND operator is "&&" and not just "&" (bitwise AND). 请注意:逻辑AND运算符是“ &&”,而不仅仅是“&”(按位AND)。

Following, your answer is YES... JVM will run conditions following your thinking. 接下来,您的答案是肯定的。JVM将按照您的想法运行条件。

By the way, I suggest you to read something abot short-circuit of these operators: it can be interesting if you are learning. 顺便说一句,我建议您阅读一些有关这些运算符短路的内容:如果您正在学习,可能会很有趣。

For example if you have if (a && (b || c) and a==false , then JVM won't evaluate b or c conditions because false && ... will be always false. 例如,如果您具有if (a && (b || c)a==false ,那么JVM将不会评估bc条件,因为false && ...始终为false。

The same in the case of if (a || b || c && d) : if a==true then JVM will ignore the other parts and consider it as true because true || .... if (a || b || c && d)的情况下相同:如果a==true那么JVM将忽略其他部分并将其视为true,因为true || .... true || .... will be always true. true || ....将永远是正确的。

Yes. 是。 if-clauses are evaluated from left to right. if子句从左到右进行评估。 If no parenthesis are used, && has precedence, even higher precedence has ! 如果不使用括号,则&&具有优先级,甚至更高的优先级具有! (not) - similar to multiplication (AND), addition (OR) and negative numbers (-) in math (eg "A && (B || C) != A && B || C == (A && B) ||C") - I recommend to use parenthesis if you are unsure. (不是)-与数学中的乘法(AND),加法(OR)和负数(-)相似(例如,“ A &&(B || C)!= A && B || C ==(A && B)| | C“)-如果您不确定,建议使用括号。 This makes it possible to combine != null checks and calls to methods in the same if statement (eg, if (object!=null && object.dosomething()) ). 这样就可以在同if (object!=null && object.dosomething()) if语句中组合!= null检查和对方法的调用(例如, if (object!=null && object.dosomething()) )。

Btw. 顺便说一句。 there is a difference between & and && ( short-circuit ), when & is used, the second condition gets evaluated even if the first is false already. &&&短路 )之间有区别,当使用&时,即使第一个条件已经为false也会评估第二个条件。 When && is used, Java doesn't check the second condition if the first one is false as the whole term cannot be true anymore - this is only important when the second condition is not a boolean variable but a method (which gets called or not -> side effects ; this "skipping" is called short-circuit). 当使用&& ,如果第一个条件为false ,则Java不会检查第二个条件,因为整个条件不再适用-这仅在第二个条件不是布尔变量而是方法(是否被调用)时才重要。 -> 副作用 ;这种“跳过”称为短路)。 Same for || ||相同 and | | where the second operator might not get evaluated if the first is already true (in case of || ). 如果第一个运算符已经为true ,则第二个运算符可能不会被求true (对于|| )。

Normally only || 通常只有|| and && are used. &&被使用。

Just for completeness: & is also the bitwise AND operator in Java. 仅出于完整性考虑: &也是Java中按位AND运算符。

if (false || operate) {  
   // some action
}

If operate true then if block executing. 如果操作为true,则执行块。

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

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