I took this code snippet from some quiz, using IDE I executed it and get a result long, long but the correct answer is Byte, Byte, why I got different result? The question is related to JDK 11
public class Client {
static void doCalc(byte... a) {
System.out.print("byte...");
}
static void doCalc(long a, long b) {
System.out.print("long, long");
}
static void doCalc(Byte s1, Byte s2) {
System.out.print("Byte, Byte");
}
public static void main(String[] args) {
byte b = 5;
doCalc(b, b);
}
}
EDITED:
The code was taken here: Oracle Certification Overview and Sample Questions (Page: 13, Question: 5)
In method overloading, the class can have multiple methods with the same name but the parameter list of the methods should not be the same. One way to make sure that the parameter list is different is to change the order of the arguments in the methods.
If both methods have the same parameter types, but different return types, then it is not possible. Autoboxing is the process of converting a primitive value into an object of the corresponding wrapper class is called autoboxing. For example, converting int to Integer class.
Overloading with same arguments and different return type −The return type doesn't matter. If they don't have different parameters, they both are still considered as same method and a compile time error will be generated.
So, if you go through the Java language specification for determining method signature at compile time it will be clear :
The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase.
The second phase (§15.12.2.3) performs overload resolution while allowing boxing and unboxing, but still precludes the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the third phase.
The third phase (§15.12.2.4) allows overloading to be combined with variable arity methods, boxing, and unboxing.
So, from the above steps, it is clear that in your case at first phase Java compiler will find a matching method which does doCalc(long a,long b)
. Your method doCalc(Byte s1, Byte s2)
needs an autoboxing during the call so it will get less preference.
Please have a read of JLS chapter on conversions.
What is happening in your case is that during runtime, JVM chooses to perform a widening conversion byte -> long
as this is conversion is safer because it is guaranteed that it does not cause RuntimeException
.
Converting from byte
to Byte
also called boxing can result in OutOfMemoryError as the JVM has to allocate new objects onto the heap:
A boxing conversion may result in an OutOfMemoryError if a new instance of one of the wrapper classes (Boolean, Byte, Character, Short, Integer, Long, Float, or Double) needs to be allocated and insufficient storage is available.
Because of that, the safer byte -> long
widening conversion is preferred.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With