What is Implicit casting/promotion of primitive Data type with programs in java



Implicit casting/promotion of primitive Data type in java >
It’s very important from ocjp exam point of view.


Diagram of Implicit casting/promotion of primitive Data type in java >



Tabular form Implicit casting/promotion of primitive Data type in java >
Primitive Data type
Can be implicitly casted/promoted to
byte
short, int, long, float, double
short
        int, long, float, double
char
        int, long, float, double
int
             long, float, double
long
                     float, double
float
                             double
boolean
-


boolean cannot be casted implicitly or explicitly to any other datatype.

Now, let’s create programs to understand Implicit Data type casting/ promotion of primitive data types in java with method overloading in java >


Program 1.1 -
In the program, 2 is a int, 2 can be casted to double as well, but rather than casting compiler will call method with int as argument.
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class MyClass {
   static void m(double x) {
          System.out.println("double");
   }
   static void m(int x) {
          System.out.println("int");
   }
  
   public static void main(String args[]) {
          m(2);
   }
}
/*OUTPUT
int
*/

Program 1.2 -
In the program, 2 is a int, 2 can be casted to double and float as well, but rather than casting to double compiler will cast 2 to float and call method with float as argument.


Q. But why in float, why not in double?
A. Because float consumes less memory than double.
public class MyClass {
   static void m(double x) {
          System.out.println("double");
   }
   static void m(float x) {
          System.out.println("float");
   }
  
   public static void main(String args[]) {
          m(2);
   }
}
/*OUTPUT
float
*/

Program 1.3 -
In the program, 2 is a int, 2 can be casted to double but cannot be casted to short, compiler will cast 2 to double and call method with double as argument.
public class MyClass {
   static void m(double x) {
          System.out.println("double");
   }
   static void m(short x) {
          System.out.println("short");
   }
  
   public static void main(String args[]) {
          m(2);
   }
}
/*OUTPUT
double
*/

Program 1.3 -
In the program, 2 is a int, 2 can’t be casted to either short or byte, so we will face compilation error
public class MyClass {
   static void m(byte x) {
          System.out.println("double");
   }
   static void m(short x) {
          System.out.println("short");
   }
  
   public static void main(String args[]) {
          m(2);  //compilation error
   }
}
/*OUTPUT
*/


Data types are not demoted Implicitly, though they may be demoted explicitly, which may cause data loss.


Example Program 2 - If we want to demote double to int, we will have to add explicit cast. In explicit cast we lost decimal value of 2.2 and are left with 2
public class MyClass {
   public static void main(String args[]) {    
          double d=2.2;
          int i=(int) d;
          System.out.println(i);
   }
}
/*OUTPUT
2
*/




RELATED LINKS>


Arithmetic, Unary, Equality, Relational, Conditional, Bitwise, Bit Shift, Assignment, instanceof operators in detail with programs.


eEdit
Must read for you :