Examveda
Examveda

What is the output for the below code?
public class Test{
      public static void printValue(int i, int j, int k){
            System.out.println("int");
      }

      public static void printValue(byte...b){
            System.out.println("long");
      }

      public static void main(String... args){
            byte b = 9;
            printValue(b,b,b);
      }
}

A. long

B. int

C. Compilation fails

D. Compilation clean but throws RuntimeException

E. None of these

Answer: Option B

Solution(By Examveda Team)

Primitive widening uses the smallest method argument possible. (For Example if you pass short value to a method but method with short argument is not available then compiler choose method with int argument). But in this case compiler will prefer the older style before it chooses the newer style, to keep existing code more robust. var-args method is looser than widen.


This Question Belongs to Java Program >> Overriding And Overloading

Join The Discussion

Comments ( 2 )

  1. Dharm Veer
    Dharm Veer :
    2 years ago

    Y4GD

  2. Dharm Veer
    Dharm Veer :
    2 years ago

    Byev

Related Questions on Overriding and Overloading