• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jason Attin
Ranch Hand
Posts: 232
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){}
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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: 232
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.
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic