1

Given the following code:

public class Clazz {

    private static String foo(Integer value) {
        return "Integer";
    }

    private static String foo(float value) {
        return "float";
    }

    public static void main(String[] args) {
        System.out.println(foo(10));
        System.out.println(foo(10f));
    }

}

Why does it print this?

float
float

I would expect the following output:

Integer
float
  • 5
    Because you use a wrapper. Type widening takes predecence before Wrapper classes. – Turing85 May 03 '18 at 15:30
  • 1
    It's interpreting your number as a float rather than autoboxing it to an `Integer`. If you overloaded for `int` instead of `Integer`, you would get the output you expect. – khelwood May 03 '18 at 15:30

2 Answers2

0

Integer is not a primitive as float.

To get what you want, you should use:

System.out.println(foo(new Integer(10)));
Raphael M.
  • 140
  • 6
-1

Try that. i have a feeling its not the correct type

Interget interger = 10;
System.out.println(foo(interger));
System.out.println(foo(10f));