aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Conditional 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 "Conditional operator Watch "Conditional operator New topic
Author

Conditional operator '?'

anand phulwani
Ranch Hand

Joined: Sep 10, 2005
Posts: 242
Will This Code Compile?


Assume that out1 and out2 have method signature: public void out1(); and public void out2();

What i thought that this won't compile because the methods have a void return type,then how will println work??

but it will compile
Here's What Explanation is Given,but its too difficult for me to understand.
So if anybody could help me with easy to understand explanation.


Note that it is not permitted for either the second or the third operand expression to be an invocation of a void method.
In fact, it is not permitted for a conditional expression to appear in any context where an invocation of a void method could appear.
The first expression must be of type boolean, or a compile-time error occurs.
The conditional operator may be used to choose between second and third operands of numeric type, or second and third operands of type boolean, or second and third operands that are each of either reference type or the null type. All other cases result in a compile-time error.
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.
Otherwise, if the second and third operands have numeric type, then there are several cases:
If one of the operands is of type byte and the other is of type short, then the type of the conditional expression is short.
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.
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.
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.
If the second and third operands are of different reference types, then it must be possible to convert one of the types to the other type (call this latter type T) by assignment conversion (5.2); the type of the conditional expression is T. It is a compile-time error if neither type is assignment compatible with the other type.
At run time, the first operand expression of the conditional expression is evaluated first; its 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. The operand expression not chosen is not evaluated for that particular evaluation of the conditional expression.


Thanks,
With Regds,
Anand


Thanks and Regards, Anand
SCJP 5.0 310-055 73%, SCWCD 1.4 310-081 78%, IBM DB2 9 Fundamentals 000-730 62%
Mostafa Radwan
Greenhorn

Joined: Sep 09, 2003
Posts: 25
Hi anand,
I think it doesn't need such long explanation. think about it in experissions so [i<20 ? out1() : out2() ] is an expression that is passed to a function that takes a String type as an input but when you look to the output of the expression it will be void which is not an acceptable input to println().

Take a look at this code snippet:


Hope this helps,
[ September 19, 2005: Message edited by: Mostafa Radwan ]

- Mostafa Radwan -
M.Sc | SCJP 1.4
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Anand, please do try to compile the code in your example. You will then find out if you are correct or not. I have just done so, and know the answer.


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

Joined: Sep 10, 2005
Posts: 242
Thanks Barry,

For telling me that i was right from the beginning,
and Thank You For All Your Support,and being always there.
By The Way I Sometimes Look Out That,
What do you get for moderating the forum,
and most of all how come you are always there
and when do you sleep.

Anyways i found out that QID:958014628100 in JQPLUS Shows Wrong Awnser.

Thanks,
With Regds,
Anand
 
wood burning stoves
 
subject: Conditional operator '?'