Hey Guys, I am creating this blog to share deep knowledge in Java, JSP, Servlets, JDBC, Hibernate, Spring and Spring MVC in details.

Explaination of method Overloading and Type Promotion in Java (Very importent for MNC tricky question)


Explaination of method Overloading and Type Promotion in Java (Very importent for MNC tricky question)
Boolean---->Byte ---->Char ---->Short ---->int ---->long ---->float ---->double

Type promotion is an automatic type conversion from a "lesser" base type to a "greater" one. Let us see the diagram given below and understand it.

It must be clear from the above diagram that byte can be promoted to short, int, long, float or double. The short data type can be promoted to int, long, float or double. The char data type can be promoted to int, long, float or double and so on.
One type is promoted to its greater type implicitly it no matching data type is found.

 a)  Example of method overloading with Type Promotion:

public class TypePromotion {
     void add(float a, float b) {
          System.out.println(a + b + " float");
     }
     void add(long a, long b) {
          System.out.println(a + b + " long");
     }
     public static void main(String args[]) {
          TypePromotion obj = new TypePromotion();
          obj.add(21, 21); // integer value is passed
     }
}

Output: 42 long
In the above example, integer value is passed through obj.add(21,21) which will be implicitly promoted to its nearest bigger type i.e. long type.
b) Example of method overloading with Type Promotion:

class TypePromotion {
     void add(float a, float b) {
          System.out.println(a + b + " float");
     }
     void add(double a, double b) {
          System.out.println(a + b + " double");
     }
     public static void main(String args[]) {
          TypePromotion obj = new TypePromotion();
          obj.add(21, 21); // integer value is passed
     }
}

Output: 42.0 float

In the above example, an integer value is passed through obj.add(21,21) which will be implicitly promoted to its nearest bigger type i.e. float type. [Note: nearest bigger type of int is long which is not matched in the above program, therefore next bigger type is matched and is promoted to float implicitly]

c) Example of method overloading with Type Promotion

public class OverloadingExm {
     void sum(int a, int b) {
          System.out.println("int arg method invoked");
     }
     void sum(long a, long b) {
          System.out.println("long arg method invoked");
     }
     public static void main(String args[]) {
          OverloadingExm obj = new OverloadingExm ();
obj.sum(20, 20);// now int arg sum() method gets invoked
     }
}

Output: int arg method invoked

d) Example of method overloading with Type Promotion giving ambiguity

class TypePromotion {
     void add(int a, long b) {
          System.out.println(" 1st method invoked");
     }
     void add(long a, int b) {
          System.out.println(" 2nd method invoked");
     }
     public static void main(String args[]) {
          TypePromotion obj = new TypePromotion();
          obj.add(21, 21);
     }
}

Compile time error will be generated because one-type cannot be demoted to any type implicitly.

Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method add(int, long) is ambiguous for the type TypePromotion at com.pcpl.ccl.TypePromotion.main(TypePromotion.java:12)
Share:

Related Posts:

No comments:

Post a Comment

Popular Posts