• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A question on overloading

 
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have a question on overloading. I got it on SCJP 6 mock test, but I think it can be included into OCA test as well. This code does not compile. The explanation for the questions was too brief, so I did not understand it. Could someone 1) help to gain understanding of it, 2) provide more examples when the compiler generate errors messages?

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you add a third method, it does compile:


Do you see why this compiles? It is because this one is an exact match. In the absence of an exact match, the compiler has to figure out what you want. Normally, it says autoboxing (Integer) takes precedence over varargs. However when it needs to use both, it sort of throws up its hands in defeat.

By the way, the OCA doesn't ask such tricky overloading questions. In fact, varargs isn't on the OCA at all.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sergey,

Determining which overloaded method will be invoked is one of the hardest things on both the OCAJP7 as the OCPJP7 exams. On the OCPJP7 exam these question will even be a lot harder/trickier There are a number of rules you need to know and which can be demonstrated using a few simple code snippets.

Let's assume the main method is always the same in every example:

Here are a few examples. All these examples will compile successfully. It's up to you to determine which method will be invoked (before confirming your answer by compiling and running the examples).
















Phew! That's a lot of examples If you have answered all above examples correctly, you'll be able to have a list of rules used by the compiler to determine which overloaded method to execute. And sometimes (as in your example) the compiler can't determine which overloaded method to use (mostly when autoboxing and var-args are combined).

(Disclaimer: both autboxing/unboxing and var-args are not on the OCAJP7 exam. I added a few examples just for completeness. You'll definitely need to know if you'll take the OCPJP7 exam)
 
Sergei Zhylinski
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for the reply. I think I begin to understand. Firstly I chose the second method, that prints '"hello" + i'.
 
Ranch Hand
Posts: 31
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roel Thanks for 15 questions

I answer all of them correct except No 11th which is as below :


I have 2 questions here
1) as per i know the var args is last choice of compiler when overloading considered ( I may be wrong please correct me if i am )
2) Though var -args not in OCAJP i am curious to know why it is printing Obj address..???
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

aditya chaudhari wrote:1) as per i know the var args is last choice of compiler when overloading considered ( I may be wrong please correct me if i am )


That's correct! Are you wondering why method with var-args parameter is chosen and not the one with the Long parameter?

aditya chaudhari wrote:2) Though var -args not in OCAJP i am curious to know why it is printing Obj address..???


That's an easy one A var-args parameter is turned into an array behind the scenes. And every array in Java is also an Object, so an array has a toString method too. And with a small code snippet you can easily see what is printed when you use an array.


Not on the OCAJP7 exam, but just for completeness: you can use the java.util.Arrays.toString method to pretty print your array.
 
Sergei Zhylinski
Ranch Hand
Posts: 89
1
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

aditya chaudhari wrote:1) as per i know the var args is last choice of compiler when overloading considered ( I may be wrong please correct me if i am )


That's correct! Are you wondering why method with var-args parameter is chosen and not the one with the Long parameter?



The second method is not approptiate. The numeric turns by default into 'Integer'. 'Integer' and 'Long' are both subclasses of 'Number'. It is impossible to cast 'Long' to 'Integer' and vise verse. So the only method here to call is the first.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sergey Zhylinsky wrote:

Roel De Nijs wrote:

aditya chaudhari wrote:1) as per i know the var args is last choice of compiler when overloading considered ( I may be wrong please correct me if i am )


That's correct! Are you wondering why method with var-args parameter is chosen and not the one with the Long parameter?



The second method is not approptiate. The numeric turns by default into 'Integer'. 'Integer' and 'Long' are both subclasses of 'Number'. It is impossible to cast 'Long' to 'Integer' and vise verse. So the only method here to call is the first.


Spot-on!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic