Method Overloading Automatic type Conversion in Java. Many interviews may I ask you Type conversion question in method overloading. If the method overloading question is asked by an interviewer then definitely below one of these types of questions may be asked by interview.
First, we need to look at what is Implicit type conversion in Java.
Is a conversion performed automatically by the compiler without the Developer intervention, the type conversion is known as implicit type conversion.
This type of conversion happens automatically by the compiler.
Double > Long > Float > Int > Short > Byte.
The conversion always gives priority to a greater data type. The above image is in descending order.
For Example: double is the data type given the first priority, then long, and so on.
Tricky Method Overloading Interview Question in java
Example: 1
OUTPUT:
MethodOverloading with parameter - int
MethodOverloading with parameter - double
MethodOverloading with parameter - double
If you see in the above example 3rd Case there is no float parameter and we are passing float as an argument, now what happens automatically type conversion is performed by a java compiler.
Example: 2
OUTPUT:
MethodOverloading with parameter - int
MethodOverloading with parameter - double
MethodOverloading with parameter - double
If you see in the 3rd case we are trying to give Integer value as an argument to double parameter here compiler will automatically do the type conversion for us from int to double.
Example: 3
OUTPUT: MethodOverloading with parameter - float
If you see there is no int parameter and here we passing int as an argument so in this case it will choose the deepest method first. As you can see int type is smaller from double and float. It will choose the most specific method to refer this Hierarchy.
Example: 4
OUTPUT: Compile Time Error
If we are passing an argument 1.5 and not assign for double d and for float f it will consider as double only automatically by java compiler. As you see in the above example. In this case, java will not make narrowing conversions automatically, because data is at risk of being lost.
Example: 5
OUTPUT: Compile Time Error
As you can see in the above example you get a compile-time error, the compiler can't downcast from int to byte by itself. If you want you can downcast manually to byte.
Example: 6
OUTPUT:
MethodOverloading with Single parameter - int
MethodOverloading with Mutiple parameter - int1
MethodOverloading with Mutiple parameter - int5
MethodOverloading with Mutiple parameter - int20
MethodOverloading with Mutiple parameter - int4
MethodOverloading with Mutiple parameter - int1
MethodOverloading with Mutiple parameter - int5
MethodOverloading with Single parameter - int
As you can see the first method it will take a single parameter of int type and the second method will take multiple parameters of int type. Tricky Method Overloading Interview Question in java
First, we need to look at what is Implicit type conversion in Java.
Implicit type conversion:
Is a conversion performed automatically by the compiler without the Developer intervention, the type conversion is known as implicit type conversion.
This type of conversion happens automatically by the compiler.
Double > Long > Float > Int > Short > Byte.
Implicit type conversion |
The conversion always gives priority to a greater data type. The above image is in descending order.
For Example: double is the data type given the first priority, then long, and so on.
Tricky Method Overloading Interview Question in java
NOTE:
Only built-in types have implicit conversions.
You cannot perform implicit conversions of your custom types.
Primitive types only have implicit conversions.
Example: 1
public class MethodOverloading { public static void overloadingMethod(int num){ System.out.println("MethodOverloading with parameter - int"); } public static void overloadingMethod(double num) { System.out.println("MethodOverloading with parameter - double"); } public static void main(String[] args) { overloadingMethod(1); overloadingMethod(10.5d); //3rd Case overloadingMethod(10.5f); } }
OUTPUT:
MethodOverloading with parameter - int
MethodOverloading with parameter - double
MethodOverloading with parameter - double
If you see in the above example 3rd Case there is no float parameter and we are passing float as an argument, now what happens automatically type conversion is performed by a java compiler.
Example: 2
public class MethodOverloading { public static void overloadingMethod(int num) { System.out.println("MethodOverloading with parameter - int"); } public static void overloadingMethod(double num,double num2) { System.out.println("MethodOverloading with parameter - double"); } public static void main(String[] args) { overloadingMethod(1); overloadingMethod(1.5,20.5); //3rd Case overloadingMethod(1,2); } }
OUTPUT:
MethodOverloading with parameter - int
MethodOverloading with parameter - double
MethodOverloading with parameter - double
If you see in the 3rd case we are trying to give Integer value as an argument to double parameter here compiler will automatically do the type conversion for us from int to double.
Example: 3
public class MethodOverloading { public static void overloadingMethod(float num) { System.out.println("MethodOverloading with parameter - float"); } public static void overloadingMethod(double num) { System.out.println("MethodOverloading with parameter - double"); } public static void main(String[] args) { overloadingMethod(1); } }
OUTPUT: MethodOverloading with parameter - float
If you see there is no int parameter and here we passing int as an argument so in this case it will choose the deepest method first. As you can see int type is smaller from double and float. It will choose the most specific method to refer this Hierarchy.
Example: 4
public class MethodOverloading { public static void overloadingMethod(float num) { System.out.println("MethodOverloading with parameter - float"); } public static void overloadingMethod(int num) { System.out.println("MethodOverloading with parameter - int"); } public static void main(String[] args) { overloadingMethod(1.5); //Compile Time Error } }
OUTPUT: Compile Time Error
If we are passing an argument 1.5 and not assign for double d and for float f it will consider as double only automatically by java compiler. As you see in the above example. In this case, java will not make narrowing conversions automatically, because data is at risk of being lost.
Example: 5
public class MethodOverloading { public static void overloadingMethod(byte num) { System.out.println("MethodOverloading with parameter - byte"); } public static void overloadingMethod(short num) { System.out.println("MethodOverloading with parameter - short"); } public static void main(String[] args) { overloadingMethod(1); //overloadingMethod((byte)1); Manually Cast } }
OUTPUT: Compile Time Error
As you can see in the above example you get a compile-time error, the compiler can't downcast from int to byte by itself. If you want you can downcast manually to byte.
Example: 6
public class MethodOverloading { public static void overloadingMethod(int num) { System.out.println("MethodOverloading with Single parameter - int"); } public static void overloadingMethod(int... num) { for (int i : num) { System.out.println("MethodOverloading with Mutiple parameter - int"+i); } } public static void main(String[] args) { overloadingMethod(1); overloadingMethod(1,5,20,4); overloadingMethod(1,5); overloadingMethod(102); } }
OUTPUT:
MethodOverloading with Single parameter - int
MethodOverloading with Mutiple parameter - int1
MethodOverloading with Mutiple parameter - int5
MethodOverloading with Mutiple parameter - int20
MethodOverloading with Mutiple parameter - int4
MethodOverloading with Mutiple parameter - int1
MethodOverloading with Mutiple parameter - int5
MethodOverloading with Single parameter - int
As you can see the first method it will take a single parameter of int type and the second method will take multiple parameters of int type. Tricky Method Overloading Interview Question in java
Post a Comment