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.");
}
}
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.
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.
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() { ... }
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).
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.
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.
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?
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.
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