There are four classes in the logical operator (in Java):
&& (short circuit and),&
|| (short circuit or), |
Short circuit and is a logical operator in the Java language, which is recorded as &&, and is similar to logic in programming languages, but has short-circuit properties. The symbol is:&&. A&&b, when a is false, returns false without calculating the value of B, and evaluates the value of B when a is true. (from Baidu Encyclopedia) and A&b, you need to calculate the values of a and B to return a value. In simple terms:&& and & are both expressed and, the difference is && if the first condition is not satisfied, the latter condition is no longer judged. & has to judge all the conditions. Example 1
Public class demo{publicstaticvoid main (string[] args) { int i = 4; if ((i++ > 6) & (i++ < 9)) { System.out.println (i); System.out.println ("If is true"); } System.out.println (i); }}
Operation Result: 6
Since I have added two times.
Example 2:
Public class demo{publicstaticvoid main (string[] args) { int i = 4; if ((i++ > 6) && (i++ < 9)) { System.out.println (i); System.out.println ("If is true"); } System.out.println (i); }}
Operation Result: 5
Since I was added once.
Similarly, | and | | is a similar situation.
Baidu Encyclopedia:
A short-circuit or a logical operator in the Java language, written as | |. The so-called short circuit, that is, when the first item is true, the second judgment is no longer made.
a| | B: Only A and B are false, the result is false, there is a true, and the result is true.
In simple terms:
|| and | Both are expressed with, the difference is | | If the first condition is not satisfied, the latter condition is no longer judged. and | You have to judge all the conditions.
,&& and &,| in Java | The difference from |