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

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

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