(The boolean variable is not initialized,but compiler doesnt give an error, is it a special case with boolean??) The answer is 3 and 1. But i dont get how j is 1. [This message has been edited by lakshmi nair (edited October 18, 2000).]
Jonathan Jeban
Ranch Hand
Joined: Oct 08, 2000
Posts: 52
posted
0
Hi lakshmi, not only booleans u can declared any primitives like that.. For example..
public class My { public static void main(String args[]) { int i = 0; int j = 0; boolean t = true; boolean r; //boolean w; int y; r = (t & 0<(i += 1)); System.out.println(i + " " + j); r = (t && 0<(i += 2)); System.out.println(i + " " + j); r = (t | 0<(j += 1)); System.out.println(i + " " + j); r = (t | | 0<(j += 2)); System.out.println(i + " " + j); //r = (w | | 0<(j += 2)); //System.out.println(i + " " + j); y = j; System.out.println(y); } }
Here note that y is not initialized. Also if we remove the commented lines , we will get compiler error... because boolean w is not initialized but used in RHS. Hope this clarifies ur doubt.. Jeban.
Dilip Nedungadi
Greenhorn
Joined: Jun 23, 2000
Posts: 3
posted
0
Jeban is right. Note that r is an automatic variable. It is not necessary to initialize an automatic variable at the time of declaration, but it should be initialized before usage, else a compiler error occurs. In contrast member variables can be used without initialization, in this case the default value of the member variable is used. Regarding your second question, observe that | | is a shortcircuit Conditional OR operator. The second operand will not be evaluated if the first operand of this operator returns true. So the second expression j+=2 is not evaluated since the first operand t is true. Change t to false and notice the difference. On the other hand | is a Boolean Logical Operator and both the operands are always evaluated. Hope this helps Thanks, Dilip [This message has been edited by Dilip Nedungadi (edited October 18, 2000).] [This message has been edited by Dilip Nedungadi (edited October 18, 2000).]