Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method overloading with primitives and their wrappers

I am trying to formulate the rules that are being used in the scenarios below. Please explain why I am getting 2 different outputs.

Scenario 1 output: I am an object.

class Test {

    public static void main (String[] args) {

        Test t = new Test(); 
        byte b_var = 10;
        t.do_the_test(b_var);
    }

    public void do_the_test(Character c) {

       System.out.println("I am a character.");
    }

    public void do_the_test(Integer i) {

      System.out.println("I am an integer.");
    }

    public void do_the_test(Object obj) {

      System.out.println("I am an object.");
    }
}

Scenario 2 output: I am an integer.

class Test {

    public static void main (String[] args) {

        Test t = new Test(); 
        byte b_var = 10;
        t.do_the_test(b_var);
    }

    public void do_the_test(char c) {

       System.out.println("I am a character.");
    }

    public void do_the_test(int i) {

      System.out.println("I am an integer.");
    }

    public void do_the_test(Object obj) {

      System.out.println("I am an object.");
    }
}
like image 956
Mohammad Ali Asgar Avatar asked Jan 14 '15 10:01

Mohammad Ali Asgar


People also ask

What is wrapper primitive type?

So a primitive wrapper class is a wrapper class that encapsulates, hides or wraps data types from the eight primitive data types, so that these can be used to create instantiated objects with methods in another class or in other classes.

Is primitive type can be widened to a wrapper class?

Primitive int type can be auto-widened to big sized primitive types or can be auto-boxed to Integer wrapper class type but can not be converted into Double or Long wrapper class type.

What is method overloading explain with suitable example?

In Java, two or more methods may have the same name if they differ in parameters (different number of parameters, different types of parameters, or both). These methods are called overloaded methods and this feature is called method overloading. For example: void func() { ... }

What is method overloading in C++?

In method overloading, you may come across a situation where a signature takes reference type or a primitive type as a formal argument. The compiler first searches a method with parameter (s) of the same data type (s).

What is method overloading with type promotion?

method overloading with Type Promotion If a class has multiple methods having same name but different in parameters, it is known as Method Overloading. If we have to perform only one operation, having same name of the methods increases the readability of the program.

Can widening and boxing of primitive types work together?

But widening and boxing of primitive type can not work together. But boxing followed by widening is acceptable if this is passed to a reference of type Object. See the following example for this.

Why is there no parameter in long wrapper method?

Since there is no method having with parameter of Long wrapper class. So, It searches for method which can accept the parameter bigger than long primitive data type as an argument. In this case, it finds a method with float primitive data type and invokes it. What happens if widening and boxing happen together?


1 Answers

The Java Language Specification says this about method signature resolution:

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.

In your second case, the method signature involving int is applicable without autoboxing, but with a widening numeric conversion. In the first case, both the widening conversion and autoboxing would be needed to reach the Integer signature; however, Java does either autoboxing or primitive conversion, never both.

like image 108
Marko Topolnik Avatar answered Nov 15 '22 15:11

Marko Topolnik