Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Overloaded conversion (Java OCA 8 Programmer I Study Guide, Sybex)

 
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, there is this example on the OCA book that I really struggle to understand and I was oping somebody can clarify it.


The book says that it can't handle conversions in steps and that's why it doesn't compile. I have absolutely no idea what that means. And, I don't understand why in play(4); java can't just convert the int 4 to a long, after all that can only call one version of the overloaded methods, public static void play(Long l){}
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
 
Henry Wong
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jason Attin wrote:I don't understand why in play(4); java can't just convert the int 4 to a long, after all that can only call one version of the overloaded methods, public static void play(Long l){}



The conversion is a little more complex than that. First, it needs to implicitly cast (widen) the int to a long, and then, it needs to autobox the long to a Long instance. This is different from the second case, where it is just an autobox.

Anyway, the allowed conversations for method calls is defined by section 5.3 of the JLS... https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.3

... and ... a widening (implicit) followed by boxing (auto) is *not* one of the allowed conversions.

Henry

 
Jason Attin
Ranch Hand
Posts: 234
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, page 295-296 of OCA study guide 1z0-808.
Ok I understand now, I think I didn't pay attention to the fact that it a 'Long' and not a 'long', so the wrapper class rather than the primitive type.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jason Attin wrote:And, I don't understand why in play(4); java can't just convert the int 4 to a long, after all that can only call one version of the overloaded methods, public static void play(Long l){}


That's really a very common mistake. And thus an all-time classic for the actual exam Many people think because an int fits into a long, an int can be converted into a Long (and/or an Integer IS-A a Long). But that's completely wrong! The compiler sees an int and thanks to autoboxing it can be converted into an Integer. And an Integer IS-NOT-A Long, so although you can cast an int to a long, you can not cast an Integer to a Long (you'll get a compiler error if you try do so).
There are many alternatives to change the statement play(4); and make it compile without errors. One is already provided (play(4L);), can you find another one (without using methods from the Long class)?

In this topic you'll find a bunch of small code snippets to determine which overloaded method will be invoked. And in this topic you'll find everything you need to know about wrapper classes (and using the == operator). Both definitely worth reading!

Hope it helps!
Kind regards,
Roel
 
Police line, do not cross. Well, this tiny ad can go through:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic