| Author |
Overloadng combined with varg args doubt.
|
David Samer
Ranch Hand
Joined: Feb 08, 2012
Posts: 44
|
|
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.
|
 |
Alexandre Leveille
Greenhorn
Joined: Jan 26, 2013
Posts: 12
|
|
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
Joined: Feb 08, 2012
Posts: 44
|
|
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
Joined: Jan 26, 2013
Posts: 12
|
|
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)
|
 |
Dan Drillich
Ranch Hand
Joined: Jul 09, 2001
Posts: 1121
|
|
I believe 5 Golden Rules of widening, boxing & varargs answers the question.
Regards,
Dan
|
William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
|
 |
Himai Minh
Ranch Hand
Joined: Jul 29, 2012
Posts: 289
|
|
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 ) .
|
 |
 |
|
|
subject: Overloadng combined with varg args doubt.
|
|
|