2
public class JavaApplication11 {

    static boolean fun() {
        System.out.println("fun");
        return true;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        boolean status = false;
        status = status & fun();
        status = fun() & status;
    }
}

Java は、status既に false であるため、funメソッドを実行しないと考えますか? どちらの場合も、fun実行されます。しかし、それはJava仕様全体で保証されていますか?

4

3 に答える 3

6

Short circuit evaluation does not happen here because you are using the bitwise and operation (&) instead of the logical and operation (&&).

于 2012-06-03T14:06:31.060 に答える
1

double & is for short circuit operations. Use

status = status && fun();
于 2012-06-03T14:06:30.050 に答える
1

オペレータは&&短絡することが保証されています。オペレーターはそうしないことが保証されています
&

于 2012-06-03T14:06:41.277 に答える