public class Typecasting {

  public static void main(String[] args) {
    byte aByte = 12;		
    short aShort = -1111;		
    int anInt = 1234567879;		
    long aLong = 123456789012L;
    
    float aFloat = 12.345f;		
    double aDouble = 23.456;
    double aDoubleE = 3.4567E-3;
    
    char aChar = 'x';

    // Some Widening Typecasts
    short bShort = aByte;
    int bInt = aShort;
    long bLong = aByte;
    double bDouble = aChar;
    
    System.out.println("byte -> short: " + bShort);
    System.out.println("short -> int: " + bInt);
    System.out.println("byte -> long: " + bLong);
    System.out.println("char -> double: " + bDouble);
    
    // Some Implicit Narrowing Typecasts (Illegal)
    /*
    byte cByte = aLong;
    char cChar = aShort;
    short cShort = aChar;
    float cFloat = aDouble;
    
    System.out.println("long -> byte: " + cByte);
    System.out.println("short -> char: " + cChar);
    System.out.println("char -> short: " + cShort);
    System.out.println("double -> float: " + cFloat);
    */
    
    // Some Explicit Narrowing Typecasts (Legal)
    
    byte dByte = (byte) aLong;
    char dChar = (char) aShort;
    short dShort = (short) aChar;
    float dFloat = (float) aDouble;
    
    System.out.println("(byte) long -> byte: " + dByte);
    System.out.println("(char) short -> char: " + dChar);
    System.out.println("(short) char -> short: " + dShort);
    System.out.println("(float) double -> float: " + dFloat);
    
    // Round Trip double -> int -> double
    double eDouble = 1234567890123.456789;
    int eInt = (int) eDouble;
    double fDouble = eInt;
    System.out.println("(int) double -> int, then int -> double ");
    System.out.println("\tWas: " + eDouble + " Now:" + fDouble);
    
  }
}