Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing byte arguments to overloaded method

Tags:

java

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)

like image 306
boden Avatar asked Oct 07 '19 12:10

boden


People also ask

Does order of arguments matter in function overloading?

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.

How is Autoboxing applied in method overloading?

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.

Does return type matters while overloading?

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.


2 Answers

So, if you go through the Java language specification for determining method signature at compile time it will be clear :

  1. 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.

  2. 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.

  3. 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.

like image 103
Amit Bera Avatar answered Oct 01 '22 05:10

Amit Bera


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.

like image 23
diginoise Avatar answered Oct 01 '22 04:10

diginoise