• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ternary operator doubt

 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
For the below code the output is compilation fails. Can anybody explain me why?


public class ObjectTest {

public static void main(String[] args) {
byte b = 0;
(true) ? b = b + 1 : b = 100;

System.out.println("Output: " + b);
}
}
Source: TeamTesting
Thanks All
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
have you tried compiling it? What does the error message say? That error message usually give a pretty good idea as to why the compilation fails.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Case #1: The leftside of the assignment must be variable. This is the reason behind compiler error.

One more thing (not part of compiler error until Case #1 is satisfied)

byte b1 =10;
b1 =b1+1; //error, can't cast from int to byte

byte + int results int, so you need explicit casting.



Thanks,
 
Ranch Hand
Posts: 37
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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;
here it seems to be that we must provide a variable which holds the returned value(obviously we have to do type cast if return type is different).

Case # 2: is correct, but again it creates the problem, if i write above code like this-

It gives compiler error. Don't know why?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Case #1: The leftside of the assignment must be variable. This is the reason behind compiler error.



[Sanjay]:


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;




That was compiler generated error, saying the left side of the assignment must be variable that is what you did:

The form of ternary operator is:

boolean expression? if true : else part

BTW,
try this:

[ May 08, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assignment operator has least precedence.

Try
b = (true) ? (b = (byte) (b + 1)) : (b = (byte)50);
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Assignment operator has least precedence.

Try
b = (true) ? (b = (byte) (b + 1)) : (b = (byte)50);



Hi Swarna,
I have two doubts look at the below statements

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?.


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.
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Assignment operator has least precedence.

Try
b = (true) ? (b = (byte) (b + 1)) : (b = (byte)50);





Hi Swarna,
I have two doubts look at the below statements

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?.


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.
 
swarna dasa
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at this code as well.



Does this fail?

Anyway sticking to your question.
Question 1:
b=(true) ? b=(byte) (b+1) : (b=50);

You have a ternary operator when you say
b=(true) ? b=(byte) (b+1) : b=50;

It is seen by the compiler as follows:

b=((true) ? b=(byte) (b+1) : b)=50;
since it is a ternary operator
after ? the expression till : is evaluated as one expression and = is left out.

This expression works fine
b = true ? b = 50:b; //Though b=50 is not in paranthesis as ? till : is evaluated

Question 2:
b=(true)
No. Reaon you have ?, compiler knows that it is a ternary operator hence evaluates the condition ? and :.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?.



In the case where there is no parenthesis, the expression is seen like this

b =((true)? b=(byte)(b+1):b) =50;

and therefore the error.

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.


You are not assigning 'true' to b instead the result of the expresion.

Ternary operator:
variable = (condition) ? expression1 : expression2

Depending on whether the condition evaluates to a true or false, assign the value of expression1 or expression2 to the variable.

In your example the condition evaluates to True and therefore the first expression's result gets assigned to b.
[ May 08, 2007: Message edited by: M Krishnan ]
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It is because the compiler sees the above expression as (see the brackets enclosing (b1 && b2) )
and therefore considers '=false' as out of place.

Instead the following might work enclose ( b2=false) in brackets)

[ May 08, 2007: Message edited by: M Krishnan ]
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Krishnan and Swarna
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



Again, the issue of = operator, least priority operator;
&& operator finds both side of the operand boolean and does its job

b1 && b2 = false
then
false = false // error

So make it like this:
if(b1 && (b2=false) || b3) { System.out.println("test");

In this case expression inside () operator is evaluated first and then
everything goes fine.


Thanks,
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everyone,
thanks for such beautiful explainations.
my doubt has been clarified.
 
swarna dasa
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Posted by Krishnan


It is because the compiler sees the above expression as (see the brackets enclosing (b1 && b2) ) and therefore considers '=false' as out of place.


I think (b1 && b2) is wrong.

Posted by Chandra


&& operator finds both side of the operand boolean and does its job

b1 && b2 = false
then
false = false // error



Having && on the left side of the = operator is the problem.

I dont think we can have any operator to the left hand side of assignment operator.

e.g.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic