File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Conditional Operator" Watch "Conditional Operator" New topic
Author

Conditional Operator

Kamil Wojcik
Ranch Hand

Joined: May 19, 2008
Posts: 78

Hi,


the output is: java.lang.Double 0.0

Explanation to that question is:
" (...) if the second and third operands have types that are convertible to numeric types, then (...) binary numeric promotion 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 and value set conversion."
.

and it's not clear for me... I thought that because Object default is NULL (not true value) second condition will execute.

source: "Java Killers"

SCJP 6.0 Passed | SCWCD Passed | PSM in progress...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14144
    
  18

"Java Killers", do you mean Paul Szulc’s Blog? Please include that, so that others can easily find it too.

The blog post contains an explanation. The two values new Integer(0) and new Double(0.0) are unboxed and promoted to the widest of the two types - double in this case. Because of the true before the ?, the first value is selected (new Integer(0), unboxed and converted to a double). It is then boxed again, because it is assigned to an Object. The result is a Double object with the value 0.0.

It's very confusing, because it looks as if the new Double(0.0) after the : is selected, but that's not what happens.

Interesting puzzle...

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Interesting puzzle...


It gets curiouser and curiouser.

Kamil, if you modify your code like this, then you will get the expected results:


SCJP 6
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14144
    
  18

Well, it's not a surprise that you then get the expected results. The variables object1 and object2 are of type Object, which is not convertible to a numeric type, so the whole unboxing / numeric promotion / boxing doesn't apply.
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Jesper Young wrote:Well, it's not a surprise that you then get the expected results. The variables object1 and object2 are of type Object, which is not convertible to a numeric type, so the whole unboxing / numeric promotion / boxing doesn't apply.


Yes, good point. I had thought runtime type would trump compile time type. Or does that come into play at all?
Anyway, I took your tip and made a second modification to Kamil's code, which indeed brings us back to the original situation with unboxing and boxing. The output is: java.lang.Double 0.0 again.


Kamil Wojcik
Ranch Hand

Joined: May 19, 2008
Posts: 78

Jesper Young wrote:"Java Killers", do you mean Paul Szulc’s Blog? Please include that, so that others can easily find it too.


Yes, the question is from Paul's blog.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14144
    
  18

Larry Chung wrote:Yes, good point. I had thought runtime type would trump compile time type. Or does that come into play at all?

No, the runtime type is not considered - the compiler only looks at the compile-time type (it can't know the runtime type at compile-time). In general, the runtime type is not used for these kind of checks, because it means that extra code would have to run that does those checks at runtime, which would make the program run slower.

One exception where Java does add a runtime check is with arrays. You could for example write the following:

This compiles, because the static type of data is array of Object. The runtime type, however is array of String, so you should not be able to store an Integer in this array. If you run this, you get:
Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer
at org.jesperdj.sandbox.JavaRanch.main(JavaRanch.java:6)

Java does a check at runtime (the array store check) every time you store an object in an array. Some people have criticized this, because it makes arrays in Java less efficient than in for example C or C++, where no such runtime check is done.
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Jesper Young wrote:
No, the runtime type is not considered - the compiler only looks at the compile-time type (it can't know the runtime type at compile-time).


I hope you were just joking.
Aditya Kanitkar
Ranch Hand

Joined: Aug 08, 2009
Posts: 72
Jesper Young wrote:It's very confusing, because it looks as if the new Double(0.0) after the : is selected, but that's not what happens.


Agreed. Even if you replace new Double(0.0) with new String("TEST"), you'll get the answer as java.lang.Integer() 0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Conditional Operator