Questions about operators in java? -
i have 1 class:
public class prog3 { public static void main(string[] arg){ int k = 0, = 0; boolean r , t = true; r = (t & 0 < (i +=1)); r = (t && 0 < (i +=2)); r = (t | 0 < (k +=1)); r = (t || 0 < (k +=2)); system.out.println( i+ " " + k); } }
why results of program are: 3 1
&&
, ||
short circuit operators, means:
- in
&&
, if left sidefalse
, right side not evaluated. - in
||
, if left sidetrue
, right side not evaluated.
while &
, |
bitwise operators, means both sides evaluated.
with @ hand, let's perform each operation:
r = (t & 0 < (i +=1)); //true & +=1 -> = 1 r = (t && 0 < (i +=2)); //true && i+=2 -> = 3 r = (t | 0 < (k +=1)); //true | k +=1 -> k = 1 r = (t || 0 < (k +=2)); //true || ... no need evaluate right side
Comments
Post a Comment