Transferred from: http://blog.csdn.net/lishiyuzuji/article/details/8116516
In Java's logical operators, there are four classes of:&& (short circuit and), &,|,| | (short circuit or).
&& and & are both expressed and, the difference is && as long as the first condition is not satisfied, the latter condition is no longer judged. & has to judge all the conditions.
Look at the following program:
[HTML]View plain copy print?
- public static void Main (string[] args) {
- TODO auto-generated Method Stub
- if ((23!=23) && (100/0==0)) {
- System.out.println ("The operation is not a problem. ");
- }else{
- <span style="White-space:pre"> </span>system.out.println ("no Error");
- }
- }
The output is "no error." Instead of changing && to &, the error will be as follows:
[HTML]View plain copy print?
- Exception in thread "main" java.lang.ArithmeticException:/By zero
The reason is:&& when the first condition is false, the latter 100/0==0 the condition is not judged.
& to judge all the conditions, so the following conditions will be judged, so will error.
|| and | are all expressions "or", the difference is | | As long as the first condition is met, the following conditions are no longer judged, and | All conditions are judged.
Look at the following program:
[HTML]View plain copy print?
- public static void Main (string[] args) {
- TODO auto-generated Method Stub
- if ((23==23) | | | (100/0==0)) {
- System.out.println ("The operation is not a problem. ");
- }else{
- <span style="White-space:pre"> </span>system.out.println ("no Error");
- }
- }
The output "operation is no problem" at this time. If the | | Instead, the error will be.
The reasons are: | | Judging the first condition is true, the following conditions do not judge the code in parentheses, and | To judge all the conditions,
So the error will be.
"Go" Java in,&& and &,| | The difference from |