Data type casting

In this chapter you will learn:

  1. Compiler error for not compatible type
  2. How to cast my type to another type

Not compatible type and casting

char and boolean are not compatible with each other.

public class Main {
  public static void main(String[] argv) {
    char ch = 'a';
    int num = 99;
    ch = num;//from   j  av a  2s  .  co m

Compiling the code above will generate the following error message.

To make it work we have to add casting before the assignment.

public class Main {
  public static void main(String[] argv) {
    char ch = 'a';
    int num = 99;
    ch = (char)num;
  }/*from   j a va 2s  .co  m*/

Casting Incompatible Types

A narrowing conversion is to explicitly make the value narrower. A cast is an explicit type conversion. It has this general form:

(target-type) value

target-type specifies the desired type. The following code casts int type value to byte type value.

public class Main {
  public static void main(String[] argv) {
    int a = 1234;
    byte b;//from  j a v  a2  s  . c  om

    b = (byte) a;

    System.out.println("a is " + a);
    System.out.println("b is " + b);

The output:

Truncation happens when assigning a floating-point value to an integer value. For example, if the value 1.23 is assigned to an integer, the resulting value will simply be 1.

public class Main {
/*j a  v  a2s  .c  o  m*/
  public static void main(String args[]) {
    byte b;
    int i = 1;
    double d = 1.123;

    System.out.println("Conversion of double to int.");
    i = (int) d;
    System.out.println("d: " + d + " i: " + i);

    System.out.println("Conversion of double to byte.");
    b = (byte) d;
    System.out.println("d: " + d + " b: " + b);


This program generates the following output:

Next chapter...

What you will learn in the next chapter:

  1. What is Automatic Type Promotion in Expressions
  2. How to fix the compile-time errors caused by automatic type promotions
  3. What is Java Type Promotion Rules
Home » Java Tutorial » Primitive Data Types


    Java Primitive Data Types


    Java boolean type
    Java boolean type conversion
    Convert string value to boolean
    Convert boolean to string


    Java char type
    Compare two char values
    Change char case
    Java char conversion
    Java char value and its attributes


    Java byte type
    Convert Byte to String
    Convert String to byte
    Byte object constructor
    Byte's max and min value
    Compare two byte values
    Convert Byte to byte, double, float, int, long and short


    Java short type
    Short min/max value and size
    Create Short object
    Compare short values
    Convert short to String
    Convert Short to primitive types
    Convert string to short
    Reverse bytes


    Java int type
    int max/min value
    Create Java integer
    Convert int to binary, hexadecimal and octal format
    Compare integer values
    Integer sign
    Convert string to int
    Convert int to primitive types
    Convert int to String
    int bit operations


    Java long type
    Compare two long values
    Convert long to binary, hex and octal
    Convert long value to primitive types
    Convert String to long value
    Convert long to String


    Java float type
    Java float type conversion
    Predefined value for float type
    Compare two float value


    Java double type
    Deal with NaN double value
    Compare two double values
    Java double type creation and comparison
    Java double type conversion

Data Type Conversion

    Java Automatic Type Conversion and Casting
    Data type casting
    Java type promotion
    Autoboxing and auto-unboxing