Tuesday, June 6, 2017

Java Basics: Type Casting



What do we mean by "Type Casting"?
Type casting is basically the process of changing or converting one variable from one data type to other different data types. For example, we may want to convert an integer variable data type to a long data type. For the primitive data types, The type casting can be performed only on the numbers. We can think of a char data type as a number since it is one of the Integral Data Types. We cannot perform casting on boolean.

What is an Integral Data Type?
An integral data type is a data type that represents a subset of the infinite integer set (such as byte, short, int, char, long in Java Language).
For the primitive data types, we have two types of conversion:
  • Narrowing primitive conversion
  • Widening primitive conversion
Before we continue with the meaning of each one, we have to know few things. Java data types are grouped into 3 groups, The Integral Types, Floating and the boolean. The integral group contains the types that does not have a decimal point. It includes the int, byte, short, long and the char. The floating group contains the types that has a decimal point. The floating group contains two data types, the double an the float. The boolean group contains only the boolean data type. Each data type has a specific size. The double is 64 bits, the long is 64 bits, the float is 32 bits, the int is 32 bits, the short is 16 bits, the char is 16 bits and the byte is 8 bits.

The next table shows the summary of data types sizes.

GroupData TypeSize
Floatingdouble64 bits
float32 bits
Integrallong64 bits
int32 bits
short and char16 bits
byte8 bits


Widening Primitive Conversion

Sometimes called Automatic Type casting. It can happen when we try to cast a small size type to a larger size type. For example, we may want to convert from a short to an int. Also we can cast from Integral type to floating type in widening primitive conversion.

GroupData TypeSize-
Floatingdouble64 bits
Widening Primitive Conversion 1
float32 bits
Integrallong64 bits
int32 bits
short and char16 bits
byte8 bits
This type of casting does not require any special thing in order to be completed. The next code example shows how it can be done.





public class Main{
  public static void main(String [] arguments){

    //the original value
    byte myByte = 65;

    //changing from byte to short
    short byteAsShort = myByte;

    //changing from byte to integer
    int byteAsInt = myByte;

    //changing from byte to double
    double byteAsdouble = myByte;

    //from long to double. Notice the 'L'
    double longAsDouble = 77L;

    //from float to double. Notice the 'F'
    double floatAsDouble = 67F

    System.out.println("byte = "+myByte);
    System.out.println("byte as short = "+byteAsShort);
    System.out.println("byte as integer = "+byteAsInt);
    System.out.println("byte as double = "+byteAsdouble);
    System.out.println("byte as double = "+byteAsdouble);
    System.out.println("long as double = "+longAsDouble);
    System.out.println("float as double = "+floatAsDouble);
  }
}

Widening Primitive Conversion 2










One thing to notice about widening primitive conversion is when we try to cast a byte or a short to a char. In order to cast from byte to char, we have to till the compiler that we are changing from byte to char. Same thing apply when we are casting from short to char. The following code example shows how to do that.





public class Main{
  public static void main(String [] arguments){

    //the original value
    byte myByte = 65;
    //changing from byte to short
    short byteAsShort = myByte;
    //changing from byte to character. Notice the '(char)' thing that we have added
    char byteAsChar = (char)myByte;
    System.out.println("byte = "+myByte);
    System.out.println("byte as short = "+byteAsShort);
    System.out.println("byte as char = "+byteAsChar);
  }
}

Widening Primitive Conversion 3









Narrowing Primitive Conversion

Narrowing primitive conversion is the opposite of Widening primitive conversion. What we do is we convert a larger size data type to a smaller one. To complete this task, we have to tell the compiler explicitly about the conversion. We do that by doing the same thing when we wanted to convert from byte to char.

GroupData TypeSize-
Floatingdouble64 bits
Narrowing Primitive Conversion 1
float32 bits
Integrallong64 bits
int32 bits
short and char16 bits
byte8 bits

The following code example shows how it is done.





public class Main{
  public static void main(String [] arguments){

    //the original value 
    double myDouble = 65.3456;
    //changing from double to float 
    float doubleAsFloat = (float)myDouble;

    //changing from double to long
    long doubleAsLong = (long)myDouble;

    //changing from double to int 
    int doubleAsInt = (int)myDouble;
     
    //from double to short 
    short doubleAsShort = (short)myDouble;

    //from double to char
    char doubleAsChar = (char)myDouble;

    //from double to byte 
    byte doubleAsByte = (byte)myDouble;
    System.out.println("double = "+myDouble);
    System.out.println("double as float = "+doubleAsFloat);
    System.out.println("double as long = "+    doubleAsLong);
    System.out.println("double as int = "+doubleAsInt); 
    System.out.println("double as short = "+doubleAsShort);
    System.out.println("double as char = "+doubleAsChar);
    System.out.println("double as byte = "+doubleAsByte);

  }
}

Narrowing Primitive Conversion Example Output










What Happened to the Decimal Point when We cast from float or double to a type that has no Decimal Point? When we cast from floating type to integral type, the number will be always truncated (the numbers after the decimal point will be cut down).





public class Main{
  public static void main(String [] arguments){

    //the original values
    double firstDouble = 88.49;
    double secondDouble = 88.50;
    double thirdDouble = 600.90;
    double fourthDouble = 600.0001;

    //changing from floating to integral type
    int firstDoubleAsInt = (int)firstDouble;
    int secondDoubleAsInt = (int)secondDouble;
    int thirdDoubleAsInt = (int)thirdDouble;
    int fourthDoubleAsInt = (int)fourthDouble;

    System.out.println("first double = "+firstDouble);
    System.out.println("first double as int = "+firstDoubleAsInt);
    System.out.println("second double = "+secondDouble);
    System.out.println("second double as int = "+secondDoubleAsInt);
    System.out.println("third double = "+thirdDouble);
    System.out.println("third double as int = "+thirdDoubleAsInt);
    System.out.println("fourth double = "+fourthDouble);
    System.out.println("fourth double as int = "+fourthDoubleAsInt);
  }
}

Narrowing Primitive Conversion Example Output


2 comments:

  1. Great article!!
    Regarding last part, "When we cast from floating type to integral type, the number will be always rounded down". The number will be truncated.

    So, -88.5 won't be rounded down to -89 but truncated to -88.

    ReplyDelete
    Replies
    1. Thanks for your note. I just updated the text. If a number is truncated, it is kinda rounded down. -88.5 will be -88.0 if it is rounded down and will be -89.0 if rounded up. The only difference is that if a number is truncated, the .0 will be removed (e.g. 88.5 will be 88 without decimal point).

      Delete

Feel free to write any thing in your mind here 😉