Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Type compatibility

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Found the following question in https://www.examtopics.com/exams/oracle/1z0-809/view/19/ :

Given:

What is the result?
A. IT:null
B. A NullPointerException is thrown at run time.
C. A compilation error occurs.
D. IT:0.0

The answer is B - fine.
But I was playing around with the code and replaced
with


and in doing so I thought that the line



would fail to compile as a result, because we know that something like

would not compile.

but it did compile ok...

Anyone got an explanation for this? :/
 
Saloon Keeper
Posts: 4505
166
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that it has to do with boxing: an Integer can be boxed to a double, but not to a Double. And my terminology may be wrong...
 
Ranch Foreman
Posts: 427
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will take a stab.

The confusion comes because DoubleSupplier interface, while beginning with a capital 'D' because it is a name of a class or an interface (well, an interface here) does not return a Double, as the name might suggest, but a primitive double, small 'd'.

As long as the implementation supplies something that can be promoted to a double, we are good.
That would include a byte, a short, an int, a long, a float, or a double itself.

The Integer we return can be auto-unboxed (more easily done than pronounced) to an int, which is merrily converted to a double.

There is an annoying rule to remember that upcasting before boxing can't be combined in one statement, but boxing before upcasting is fine.

This means we can't say this and have it work:
jshell> byte b = 7;
b ==> 7

jshell> Long L = b;
|  Error:
|  incompatible types: byte cannot be converted to java.lang.Long
|  Long L = b;
|           ^

we also can't do this:
jshell> Byte B = 7;
B ==> 7

jshell> Long L = B;
|  Error:
|  incompatible types: java.lang.Byte cannot be converted to java.lang.Long
|  Long L = B;
|           ^

We have no problem getting a primitive double out of our Byte, however:
jshell> double d = B;
d ==> 7.0

Because a DoubleSupplier, despite the initial capital 'D' in the name only requires something that can be upcast to a double primitive, perhaps after auto-unboxing, we are all good.

I agree this is a bit confusing.
 
Jay Chung
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:I think that it has to do with boxing: an Integer can be boxed to a double, but not to a Double. And my terminology may be wrong...



Ah yes of course! I forgot that DoubleSupplier's abstract method returns double rather than Double!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic