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 side false, right side not evaluated.
  • in ||, if left side true, 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

Popular posts from this blog

.htaccess - htaccess convert request to clean url and add slash at the end of the url -

C# random value from dictionary and tuple -

algorithm - Testing tetrahedron-triangle intersection -