• 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

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
i got a question like this
System.out.println(5 > 0 ? true ? 2 != 3 : "bar" : 3 <= 6 ? false : 0);

the answer is true, but i thought it should be a compilation error as the return types are incompatibles for true and false..
can anybody explain in what all conditions incompatible types will work for ternary operators like this case..

i'm a bit confused.
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i got a question like this
System.out.println(5 > 0 ? true ? 2 != 3 : "bar" : 3 <= 6 ? false : 0);



Following code may clear your confusion

if(5 > 0)
return true;
else if(2 != 3)
return "bar";
else if(3<=6)
return false;
else
return 0;
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the syntax for ternary


[ April 30, 2007: Message edited by: M Krishnan ]
[ April 30, 2007: Message edited by: M Krishnan ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

can anybody explain in what all conditions incompatible types will work for ternary operators like this case..



I believe the reason that this works is because the system.out.print takes an Object as a parameter, and with autoboxing, it is a common type for both types of the ternary operator.

Henry
 
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 not working for me. I am getting a compiler error.

I am using Java 1.5
[ April 30, 2007: Message edited by: M Krishnan ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using a compiler before 1.5? Before 1.5, incompatible types could occur with the ternary operator. But in 1.5, there are specific rules that define what happens when the operands have different types.

This is from the Java Language Specification 15.25.

The type of a conditional expression is determined as follows:

* If the second and third operands have the same type (which may be the null type), then that is the type of the conditional expression.
* If one of the second and third operands is of type boolean and the type of the other is of type Boolean, then the type of the conditional expression is boolean.
* If one of the second and third operands is of the null type and the type of the other is a reference type, then the type of the conditional expression is that reference type.
* Otherwise, if the second and third operands have types that are convertible (�5.1.8) to numeric types, then there are several cases:
o If one of the operands is of type byte or Byte and the other is of type short or Short, then the type of the conditional expression is short.
o If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression of type int whose value is representable in type T, then the type of the conditional expression is T.
o If one of the operands is of type Byte and the other operand is a constant expression of type int whose value is representable in type byte, then the type of the conditional expression is byte.
o If one of the operands is of type Short and the other operand is a constant expression of type int whose value is representable in type short, then the type of the conditional expression is short.
o If one of the operands is of type; Character and the other operand is a constant expression of type int whose value is representable in type char, then the type of the conditional expression is char.
o Otherwise, binary numeric promotion (�5.6.2) is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands. Note that binary numeric promotion performs unboxing conversion (�5.1.8) and value set conversion (�5.1.13).
* Otherwise, the second and third operands are of types S1 and S2 respectively. Let T1 be the type that results from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2. The type of the conditional expression is the result of applying capture conversion (�5.1.10) to lub(T1, T2) (�15.12.2.7).

At run time, the first operand expression of the conditional expression is evaluated first; if necessary, unboxing conversion is performed on the result; the resulting boolean value is then used to choose either the second or the third operand expression:

* If the value of the first operand is true, then the second operand expression is chosen.
* If the value of the first operand is false, then the third operand expression is chosen.

The chosen operand expression is then evaluated and the resulting value is converted to the type of the conditional expression as determined by the rules stated above. This conversion may include boxing (�5.1.7) or unboxing conversion. The operand expression not chosen is not evaluated for that particular evaluation of the conditional expression.


Conditional Operator ? :
 
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
Keith,

what is the order of evaluation in the following statement?
[ 5 > 0 ? true ? 2 != 3 : "bar" : 3 <= 6 ? false : 0);]


Is the following correct?
5 > 0 ? ( true ? 2 != 3 : "bar") : ( 3 <= 6 ? false : 0)


From JLS:


The conditional operator is syntactically right-associative (it groups right-to-left), so that a?b:c?d:e?f:g means the same as a?b : ( c?d : ( e?f:g)).



But it does not exactly resemble the example given in the JLS - a?b:c?d:e?f:g.

Thanks
[ April 30, 2007: Message edited by: M Krishnan ]
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that's correct.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interestingly the type of the expression is a Boolean not a boolean.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

This is the original line from Gaurav, only with indentations

The result of the expression is arrowed.

Wow, Barry, I also want such eagle eyes!
It is a Boolean because the alternative to (2 != 3) is "bar", the String is an object, so the boolean gets boxed.
Right?

By the way, compiling: also Eclipse 3.1 that has a java5 compiler of its own waves the red (white?) flag and says: incompatible operand types. The original compiler from sun works fine.

Yours,
Bu.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bu wrote:

By the way, compiling: also Eclipse 3.1 that has a java5 compiler of its own waves the red (white?) flag and says: incompatible operand types. The original compiler from sun works fine.



It's the same with Eclipse 3.2.2 with Java 6.0. I think it's correct - the least upper bound of boolean autoboxed to Boolean and String is Object (not Boolean). In Eclipse you can assign the expression to a variable of type Object but not Boolean.

(we can now await JayWhy to descend upon us to impart great words of wisdom )
 
Clowns were never meant to be THAT big! We must destroy it with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic