[!INDEX]

  1. (float, float) = (int, float) & (float, int)
  2. (int, int) = (int) & (int... a)
  3. (int,int,int) = (int) & (int... a)
  4. ( ) + (null) = (int) & (int... a)
  5. (int) = (float) & (double) & (long).
  6. (int) = (float) & (double)
  7. (int) = (float) & (string)
  8. (byte) & (char) & (short) = (int) & (string)
  9. main method overloading

1 - (float, float) = (int, float) & (float, int)

class F_018_Test {
    void show(int a, float b) {
        System.out.println("int float method");
    }
    void show(float a, int b) {
        System.out.println("Float int method");
    }
    public static void main(String[] args) {
        F_018_Test t = new F_018_Test();
        t.show(10.5f, 20.5f); // This is the point of interest
    }
}

[!important]

  1. No Automatic Demotion (Narrowing):
  2. Java does not automatically demote (or narrow) larger types to smaller types because it can result in loss of data.
  3. For example:
    1. int cannot be automatically demoted to short or byte.
    2. float cannot be automatically demoted to int or long.

2 - (int, int) = (int) & (int... a)

class F_019_Temp {
    void show(int a) {
        System.out.println("int method");
    }
    void show(int... a) {
        System.out.println("varargs method");
    }
    public static void main(String[] args) {
        F_019_Temp t = new F_019_Temp();
        t.show(10, 20); // varargs works
    }
}

[!NOTE]

  1. Java prefers exact matches over varargs methods when resolving overloaded methods.

3 - (int,int,int) = (int) & (int... a)

class F_020_Temp {
    void show(int a) {
        System.out.println("int method");
    }
    void show(int... a) {
        System.out.println("varargs method");
    }
    public static void main(String[] args) {
        F_020_Temp t = new F_020_Temp();
        t.show(10, 20, 30); // multiple also works
    }
}

[!NOTE]

  1. t.show(10, 20, 30)does not match the methodvoid show(int a)because it only takes a singleint` argument.

4 - ( ) + (null) = (int) & (int... a)

class F_021_Test {
    void show(int a) {
        System.out.println("int method");
    }
    void show(int... a) {
        System.out.println("varargs method");
    }
    public static void main(String[] args) {
        F_021_Test t = new F_021_Test();
        t.show(); // method call with no arguments
        t.show(null); // calls - varargs method
    }
}
varargs method
varargs method

[!NOTE]

  1. The call t.show() matches the method void show(int... a) because it can accept zero or more int arguments.

5 - (int) = (float) & (double) & (long).

class EClass {
    void show(float a) {
        System.out.println("int to float");
    }
    void show(double a) {
        System.out.println("int to double");
    }
    void show(long a) {
        System.out.println("int to long");
    }    
    public static void main(String[] args) {
        EClass t = new EClass();
        t.show(10); // method call with an integer argument
    }
}

[!important]

  1. int to long: Exact promotion, no loss of precision.

6 - (int) = (float) & (double)

class EClass {
    void show(float a) {
    }   
    void show(double a) {
    }
    public static void main(String[] args) {
        EClass t = new EClass();
        t.show(10); // method call with an integer argument
    }
} // will promote to float but chatGPT said double

[!important]

  1. The method void show(double a) will be chosen because it provides a wider, exact match without any precision loss.

Why Some Editors Suggest float:

  1. Some editors or IDEs might suggest using float because you have an overloaded method for float, and in some cases, developers might intend to use float for memory-saving reasons. However, double is the default choice for numeric literals unless explicitly stated otherwise.

7 - (int) = (float) & (string)

class EClass {
    void show(float a) {
        System.out.println("int to float");
    }
    void show(String a) {
        System.out.println("String method");
    }
    public static void main(String[] args) {
        EClass t = new EClass();
        t.show(10); // method call with an integer argument
    }
}

[!NOTE]

  1. An int can be promoted to float because float can hold the value of an int without loss of precision.
  2. intcannot be directly promoted toString`, as they are fundamentally different types.

8 - (byte) & (char) & (short) = (int) & (string)

class EClass {
    void show(int a) {
        System.out.println("int method");
    }
    void show(String a) {
        System.out.println("String method");
    }
    public static void main(String[] args) {
        byte b = 101;
        short s = 200;
        EClass t = new EClass();
        t.show('a'); // method call with a char argument
        t.show(b);   // method call with a byte argument
        t.show(s);   // method call with a short argument
    }
}

[!NOTE]

  1. There is no method show that takes a char directly. char can be promoted to int.
  2. There is no method show that takes a byte directly. byte can be promoted to int.
  3. There is no method show that takes a short directly. short can be promoted to int.

9 - main method overloading

class EClass {
    public static void main(String[] args) {
        System.out.println("1");
        EClass t = new EClass();
        //int [] x = new int[3];
        t.main(new int[]{20});
    }
    public static void main(int[] args) {
        System.out.println("2");
    }
}

[!NOTE]

  1. The standard main method with String[] args is executed first, printing 1.
  2. This method then calls the overloaded main method with int[] args, which prints 2.

images