Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Overloading: Number,Number ; int,Double

In two days i have an exam in java, and i can not figure out the answer to this question:

class ClassA {
 public String foo(Integer x , int y) {
  return "Integer, int";
 }
 public String foo(int x, Double y) {
  return "int, Double";
 }
 public String foo(Number x, Number y) {
  return "Number, Number";
 }
 public String foo(Object x, Object y) {
  return "Object, Object";
 }
 public static void main(String... args) {
  ClassA a = new ClassA();
  System.out.print(a.foo(5, 1.2f) + " ");
  System.out.println(a.foo(null, null));
 }
}

What's the output?

The Answer is:

Number, Number Number, Number

I know that java always chooses the most specified Method, that is why a.foo(null,null); will envoke the Number,Number Method and not the Object,Object Method. But why does a.foo(5,1.2f); also envoke the Number,Number Method and not the int,Double Method??

But one more thing which might be helpful: If i remove the f after 1.2, so that the call is: a.foo(5,1.2); I get a compiler error, that it can not choose between the Number,Number and int,Double Method...

Would be really helpful, if you guys could explain that to me :)

like image 393
user1459010 Avatar asked Jun 15 '12 15:06

user1459010


People also ask

Can you add int and double in Java?

In Java, integer addition and double addition use different hardware. Even though they may seem the same to you, they are actually quite different to the computer. The computer wants to either add two int values or two double values. Java's solution (as in many other languages) is type promotion.

Can we overload method with different return type in Java?

No, you cannot overload a method based on different return type but same argument type and number in java. same name. different parameters (different type or, different number or both).

Can int store double?

We can convert int value to Double object by instantiating Double class or calling Double. valueOf() method. Let's see the simple code to convert int to Double in java.

What is Overloading in Java with 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() { ... } void func(int a) { ... }


2 Answers

1.2f is not wrapped by a Double, it's wrapped by a Float. SinceFloat is not a subclass of Double (they are distinct subclasses of Number), the most specific method signature that can be used is foo(Number,Number).

Once you remove the f, 1.2 will be treated a double (the primitive, not the wrapper class) by default, which can be autoboxed to a Double. However the 5 can also be autoboxed to an Integer, thus causing the ambiguity.

like image 81
tskuzzy Avatar answered Sep 21 '22 11:09

tskuzzy


There are two important factors here.

First, 1.2f is not a Double. It's a Float. The (int, Double) function doesn't match at all. (Number, Number) is the best fit.

Second, even when you change it to 1.2 it is still not a Double. It is a double. That is, it's a primitive, not an object. Now, Java will still happily pass a double into a function that wants a Double without much complaint, but in this case you've confused it by giving it two valid conversions it could make:

  1. Convert 5 to an Integer and convert 1.2 to a Double
  2. Leave 5 as a primitive int but convert 1.2 to a Double.

There isn't a rule for which of those is preferable. Java produces a compiler error that it has an ambiguous function call, and forces you to choose which one you'd prefer (by manually wrapping one or both of them in objects).

As an aside, if you had a method that took (int, double) there would be no ambiguity at all: that method actually matches the existing types of 5 and 1.2, so it would be called. It's the fact that some of the arguments here are wrapper objects that causes the mayhem.

like image 34
VoteyDisciple Avatar answered Sep 22 '22 11:09

VoteyDisciple