Logical AND (&), and a short circuit (&&), logical OR (|), short or (||)

Logical AND (&), and a short circuit (&&), logical OR | difference, or a short circuit (||) of ()

  • &And |: all conditions will be judged
  • &&: No longer encountered as a result of determination down to false
  • ||: Face down determination result is no longer true when

Logical AND (&)

public class Hello {
    public static void main(String[] args) {
        int i = 3 ;
        boolean flag = (i>10) & (i++)<5;
        System.out.println(flag);
        System.out.println(i);
    }
}

Since &determines all conditions, the value i is incremented by 1 and finally

false
4

Short-circuit and (&&)

public class Hello {
    public static void main(String[] args) {
        int i = 3 ;
        boolean flag = (i>10) && (i++)<5;
        System.out.println(flag);
        System.out.println(i);
    }
}

&&Encounter is false no longer be judged. Because i> 10 is false, so that (i ++) <5 irrespective of the outcome determination is not performed, the overall result is false, the value of i is still 3

false
3

Logical OR (|)

public class Hello {
    public static void main(String[] args) {
        int i = 3 ;
        boolean flag = (i>1) | (i++)<5;
        System.out.println(flag);
        System.out.println(i);  
    }
}

|Results are determined on both sides, I will add 1, the result is true, i = 4

true
4

Short or (||)

public class Hello {
    public static void main(String[] args) {
        int i = 3 ;
        boolean flag = (i>1) || (i++)<5;
        System.out.println(flag);
        System.out.println(i);
    }
}

||The result is true not judge encounter down. i> 1 the result is true, (i ++) <5 is not judged, the final result is true, i = 3

true
3

Guess you like

Origin www.cnblogs.com/zjw-blog/p/11870259.html