There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
cmbhatt
Regards, Sanjay Singh
SCJP-1.6, OCEWCD 6
Case #1: The leftside of the assignment must be variable. This is the reason behind compiler error.
Hi Chandra, your comment for Case#1 might not correct. e.g see this code, it compiles -
byte b = 0;
b = (true) ? b = (byte) (b + 1) : b;
cmbhatt
Assignment operator has least precedence.
Try
b = (true) ? (b = (byte) (b + 1)) : (b = (byte)50);
Assignment operator has least precedence.
Try
b = (true) ? (b = (byte) (b + 1)) : (b = (byte)50);
Nik wrote:
byte b=0;
b = (true) ? b= (byte) (b + 1) : b= 50; //This code does not compile;
b=(true) ? b=(byte) (b+1) : (b=50); // This code compiles
My first doubt is when parantheses is given the code compiles why it does not compile without parantheses. Whats the difference in providing parantheses and not providing in the above statements?.
nik wrote:
My second doubt look at the below statement
b=(true)
Is it a assignment statement?. If yes then b is byte how can we assign boolean to it.
debasmita
Originally posted by debasmita pattnayak:
hi,
here is my doubt regarding the following code posted by swarna
code:
--------------------------------------------------------------------------------
boolean b1=false,b2=false,b3=false; if(b1 && b2=false || b3) {System.out.println("test");}
--------------------------------------------------------------------------------
The code doesnt compile.
Can anyone explain me why?
cmbhatt
debasmita
It is because the compiler sees the above expression as (see the brackets enclosing (b1 && b2) ) and therefore considers '=false' as out of place.
&& operator finds both side of the operand boolean and does its job
b1 && b2 = false
then
false = false // error