• 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

Overloadng combined with varg args doubt.

 
Ranch Hand
Posts: 49
Eclipse IDE Spring Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings there people.

I am hell confuse regarding one exam question from a well known simulator.

This is how the code looks like:



It prints the output:

aebcd

I am having hard time understanding the calls to:

1) doNumber(2L); which calls last one doNumber(Object) , why? Im lost with "2L"

2) doNumber(new int[]{1,2,3,4}); which calls doNumber (int... dn) ( why not the doNumber(Object) ? )

3) doNumber(new Integer[3]); which calls doNumber (Integer... dn) . As far as I know Integer constructor admits String or an int as parameter so I get lost with the tricky square brackets [].

Thanks in advance.

 
Greenhorn
Posts: 12
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

1) 2L is a long and java won't automatically cast down a long to an int because there's a possible loss of precision (even though in this case, the number 2 can very well fit inside an int). What Java can do however is auto-box your long literal to a Long object. This Long object is holding the value 2, and match the arguments list for doNumber (Object dn).

Quoting Katty Sierra and Bert Bates, Chapter 3 Using Wrapper Classes and Boxing, page 249:

In every case, when an exact match isn't found, the JVM uses the method with the smallest argument that is wider than the parameter.



2) int[]{1, 2 , 3 ,4} could match doNumber (Object dn): just comment doNumber (int... dn) and it will do so!

But the method doNumber (int... dn) is considered a more direct match. That is because var-args were created to address the problem that passing an arbitrary number of values required arrays.

With the arrival of var-args, many API methods that accepted a number of arguments via an array have been changed to use var-args.
Eg.: MessageFormat.format(String pattern, Object[] arguments) is now format(String pattern, Object... arguments).

Therefore, an Array can be used where a var-args is expected, but an arbitrary number of arguments cannot be used where an Array is expected, preserving the compatibility with legacy code and APIs.

3) new Integer[3] is instantiating an Array of Integers (new Integer[]{0,0,0}). It then matchs doNumber (Integer... dn) for the same reason int[]{1, 2 , 3 ,4} matches doNumber (int... dn).
 
David Samer
Ranch Hand
Posts: 49
Eclipse IDE Spring Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alexandre Leveille wrote:Hi!

1) 2L is a long and java won't automatically cast down a long to an int because there's a possible loss of precision (even though in this case, the number 2 can very well fit inside an int). What Java can do however is auto-box your long literal to a Long object. This Long object is holding the value 2, and match the arguments list for doNumber (Object dn).

Quoting Katty Sierra and Bert Bates, Chapter 3 Using Wrapper Classes and Boxing, page 249:

In every case, when an exact match isn't found, the JVM uses the method with the smallest argument that is wider than the parameter.



2) int[]{1, 2 , 3 ,4} could match doNumber (Object dn): just comment doNumber (int... dn) and it will do so!

But the method doNumber (int... dn) is considered a more direct match. That is because var-args were created to address the problem that passing an arbitrary number of values required arrays.

With the arrival of var-args, many API methods that accepted a number of arguments via an array have been changed to use var-args.
Eg.: MessageFormat.format(String pattern, Object[] arguments) is now format(String pattern, Object... arguments).

Therefore, an Array can be used where a var-args is expected, but an arbitrary number of arguments cannot be used where an Array is expected, preserving the compatibility with legacy code and APIs.

3) new Integer[3] is instantiating an Array of Integers (new Integer[]{0,0,0}). It then matchs doNumber (Integer... dn) for the same reason int[]{1, 2 , 3 ,4} matches doNumber (int... dn).




oh I see, so in the case of 1) with "2L" argument being long, cannot be casted to int due lost of precision . Widening not happening then JVM goes for the autoboxing using wrapper class instead. That makes sense

Yet I am not having clearly enough the varg args arguments, looks like wildcard over the rest of choices.
ty
In the particular case of doNumber (int... dn) , anything that is int , then how a new Integer[] {0,0,0,0} is an it? An array like that looks like Object type rather than "int" primitive type, having (holding inside) int primitives. I yet do not see why the array is an "int" rather than "Object" instead. The same goes for Integer[3] :/

Thank you for your answer Alexandre Leveille

 
Alexandre Leveille
Greenhorn
Posts: 12
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again,

doNumber (int... dn) will be matched by new int[] {1, 2 ,3} and
doNumber (Integer... dn) will be matched by new Integer[] {0,0,0,0}

That is because the var-args will be matched by arrays of the same type. This ensure backward compatibility with old code using arrays to pass multiple values and the new APIs expecting var-args. In order to do this, the Java guys made it so that where an array is used, it will match a var-args of the same type.

In order to fully understand just how similar arrays and var-args are, type the following code in your editor and it won't compile. javac will complain that doNumbers(Integer[]) is there twice.


Now since doNumbers(Integer[] dn) is almost the same as doNumbers(Integer... dn), you should see why this is considered a better match than the generic doNumbers(Object... dn)
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe 5 Golden Rules of widening, boxing & varargs answers the question.

Regards,
Dan
 
Bartender
Posts: 2436
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Samer wrote:Greetings there people.

I am hell confuse regarding one exam question from a well known simulator.

This is how the code looks like:




One more thing to note:
doNumber(Integer ... dn) is equivalent to doNumber(Integer[] dn)

When you understand this, you will see why Integer[] i can be passed to doNumber ( Integer...dn ) .
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic