err3.java

来自「gcc的组建」· Java 代码 · 共 35 行

JAVA
35
字号
/*--------------------------------------------------------------------------*//* File name  : err3.java                                              *//*            :                                                             *//* Cause      : Evaluation sequence of the formula which used               *//*            : the substitution operator is not performed correctly.       *//*            :                                                             *//* Message    : NG1:[27]-->[9]                                              *//*            : NG2:[27]-->[9]                                              *//*            :                                                             *//* Note       : JLS 15.6 Evaluation Order (p305)                            *//*                 S15.6.1 Evaluate Left-Hand Operand First                 *//*            : A formula should be evaluated to 9*3 instead of 3*3.        *//*--------------------------------------------------------------------------*/public class err3 {  public static void main(String[] args) {    int x = 9;    x *= (x = 3);    if ( x == 27 ) {      System.out.println("OK1");    } else {      System.out.println("NG1:[27]-->["+x+"]");    }    int y = 9;    y = y * (y = 3);    if ( y == 27 ) {      System.out.println("OK2");    } else {      System.out.println("NG2:[27]-->["+y+"]");    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?