wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ternary operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ternary operator" Watch "ternary operator" New topic
Author

ternary operator

gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
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.


thanks,<br />gaurav abbi
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
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;
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
Here is the syntax for ternary


[ April 30, 2007: Message edited by: M Krishnan ]
[ April 30, 2007: Message edited by: M Krishnan ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
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 ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Aug 13, 2006
Posts: 178
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

Joined: Feb 07, 2005
Posts: 2367
I believe that's correct.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Interestingly the type of the expression is a Boolean not a boolean.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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 )
 
 
subject: ternary operator