I'm not quite sure what you are asking, but at least the bytecode is different from boolean c = a|b and boolean c = a||b :
boolean a = true; boolean b = false;
boolean c = a|b; ILOAD 1 ILOAD 2 IOR ISTORE 3
boolean c = a||b; ILOAD 1 IFNE L4 ILOAD 2 IFNE L4 ICONST_0
Thus, two operators effectively lead to different operations at the bytecode level. In particular, || evaluates only the second operand if the first operand is false and | evaluates both operands anyway:
public boolean a() { System.out.println(" a"); return true; } public boolean b() { System.out.println(" b"); return false; } public void c() { System.out.println("a() | b()"); boolean r1 = a() | b(); System.out.println("\na() || b()"); boolean r2 = a() || b(); }
Output:
a() | b() a b a() || b() a
At the same time, the bytecode for the integer bitwise or the same as for Boolean bitwise or:
int a1 = 10; int a2 = 20; int c1 = a1 | a2;
ILOAD 4 ILOAD 5 IOR ISTORE 6
source share