简体   繁体   English

Java:条件运算符的奇怪行为

[英]Java: weird behavior of conditional operator

I have a very simple java class to solve Decode ways using recursive approach.我有一个非常简单的 java class 来解决使用递归方法的解码方式。 I am seeing this weird behavior of conditional operator,我看到条件运算符的这种奇怪行为,

package decodeways;

public class Solution {
  public static void main(String[] args) {
    System.out.println(numDecodings("1456"));
  }
  
  public static int numDecodings(String s) {
    if(s.length()>0 && s.charAt(0)=='0') 
      return 0;
    if(s.length()==0) return 1;
    if(s.length()==1)
      return 1;
    
    int num1 = s.charAt(0)-'0';
    int num2 = s.charAt(1)-'0';
    int one = numDecodings(s.substring(1));
    int two = s.length()>1?numDecodings(s.substring(2)):0;
    int res = one 
        + num1<3 && num2<7 ? two:0;
    return res;
  }
}

if I put a parentheses, (num1<3 && num2<7? two:0) then everything is well and good but if I remove the parentheses, then getting incorrect results.如果我放括号, (num1<3 && num2<7? two:0)那么一切都很好,但是如果我删除括号,则会得到不正确的结果。

during the process of debugging, one will be computed to 1 and two will be computed to 1 and res will be 1 as well with parentheses but without it, the computed result of res will be 0 (screnshot attached) which is the source of error.在调试过程中,一个会被计算为1,两个会被计算为1,带括号的res也是1,但是没有括号,res的计算结果会是0(附上截图),这是错误的来源. 在此处输入图像描述 I am aware of the operator precedence in java, but in this situation I can't figure out why it shows incorrect behavior, because in the below code:我知道 java 中的运算符优先级,但在这种情况下,我无法弄清楚为什么它显示不正确的行为,因为在下面的代码中:

int res = one 
        + num1<3 && num2<7 ? two:0;

one + num1<3 is illegal So, java is intelligent enough to not confuse between (one + num1<3) and (num2<7? two:0) to be consider separately. one + num1<3 是非法的因此,java 足够智能,不会混淆 (one + num1<3) 和 (num2<7? two:0) 之间的混淆,需要单独考虑。 So, as per my understanding the only legal observable behavior for java compiler is to automatically consider num1<3 && num2<7? two:0因此,根据我的理解,java 编译器唯一合法的可观察行为是自动考虑num1<3 && num2<7? two:0 num1<3 && num2<7? two:0 as an atomic operation(Please correct me if I am wrong), irrespective of parentheses is available or not. num1<3 && num2<7? two:0作为原子操作(如果我错了,请纠正我),无论括号是否可用。

Please guide me to have a better understanding.请指导我更好地理解。

Thanks.谢谢。

int res = one 
        + num1<3 && num2<7 ? two:0;

is equivalent to相当于

int res = (((one + num1) < 3) && (num2 < 7)) ? two : 0;

Everything before ?之前的一切? is included in the boolean expression, since the ternary/conditional operator has the lowest precedence here (not including the assignment operator) and + has the highest.包含在 boolean 表达式中,因为这里三元/条件运算符的优先级最低(不包括赋值运算符),而+的优先级最高。

The order goes something like:顺序如下:

  • + , so one + num1 is put together first + ,所以one + num1先放在一起
  • < , so now there's (one + num1) < 3 and num2 < 7 < ,所以现在有(one + num1) < 3num2 < 7
  • && , after which you have ((one + num1) < 3) && (num2 < 7) && ,之后你有((one + num1) < 3) && (num2 < 7)
  • and finally ?:最后?:

You seem to be expecting the newline to make the compiler think one and num1<3 && num2<7? two:0您似乎期望换行符使编译器one num1<3 && num2<7? two:0 num1<3 && num2<7? two:0 are separate, but it actually just ignores all whitespace. num1<3 && num2<7? two:0是分开的,但它实际上只是忽略了所有空格。 Putting the parentheses there is the only way to make sure one is added to the whatever the conditional operator evaluates to.将括号放在那里one确保将括号添加到条件运算符评估的任何内容的唯一方法。

int res = one + (num1 < 3 && num2 < 7 ? two : 0);

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

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