[!INDEX]
- (float, float) = (int, float) & (float, int)
- (int, int) = (int) & (int... a)
- (int,int,int) = (int) & (int... a)
- ( ) + (null) = (int) & (int... a)
- (int) = (float) & (double) & (long).
- (int) = (float) & (double)
- (int) = (float) & (string)
- (byte) & (char) & (short) = (int) & (string)
- 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]
- No Automatic Demotion (Narrowing):
- Java does not automatically demote (or narrow) larger types to smaller types because it can result in loss of data.
- For example:
intcannot be automatically demoted toshortorbyte.floatcannot be automatically demoted tointorlong.
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]
- 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]
- 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]
- The call
t.show()matches the methodvoid show(int... a)because it can accept zero or moreintarguments.
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]
inttolong: 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]
- 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:
- Some editors or IDEs might suggest using
floatbecause you have an overloaded method forfloat, and in some cases, developers might intend to usefloatfor memory-saving reasons. However,doubleis 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]
- An
intcan be promoted tofloatbecausefloatcan hold the value of anintwithout loss of precision.- int
cannot 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]
- There is no method
showthat takes achardirectly.charcan be promoted toint.- There is no method
showthat takes abytedirectly.bytecan be promoted toint.- There is no method
showthat takes ashortdirectly.shortcan be promoted toint.
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]
- The standard
mainmethod withString[] argsis executed first, printing1.- This method then calls the overloaded
mainmethod withint[] args, which prints2.
images