[Solved] foo(int) is not applicable for the arguments (String)


For starters, you are trying to cast between an Object, String, and a primitive, int. This simply will not work. Objects cannot be cast to primitives, and vice-versa.

A String, comprises of an array of char‘s wrapped into an Object.
An int, comprises of a single signed decimal number.

When you attempt to run a method which requires an int with a String, you are not supplying it with a number, you are supplying it with a char, which then has to be cast to an int.

Besides, the way you have setup overloading, there is no way to differentiate between weather or not you are using the m1(int) method or m1(float) method. To fix this, you should add the following method:

public void m1(String s) {
    System.out.println("String-arg");
}

For the future, to cast between a String and int, use:

int i = Integer.parseInt("44");  // Equal to 44.

Which then runs the risk of giving back a NumberFormatException, so to be safe:

public void randomMethod(String input) {
    int i = 0;
    try {
        i = Integer.parseInt(input);
    } catch (NumberFormatException e) {}  // Fill in with your requirements.
}

solved foo(int) is not applicable for the arguments (String)