Java Automatic Type Conversion and Casting

In this chapter you will learn:

  1. What is data type casting and converting
  2. What are the conditions for Java's Automatic Conversions
  3. How Java compiler deals with the automatic type conversion and larger type size

Data type casting

If the two types are compatible, then Java will perform the conversion automatically. For example, assign an int value to a long variable. Since long type is larger than int type. A long type variable has enough space to hold an int value.

For incompatible types we must use a cast. Casting is an explicit conversion between incompatible types.

Java's Automatic Conversions

An automatic type conversion will be used if the following two conditions are met:

  1. The two types are compatible.
  2. The destination type is larger than the source type.

int type is always large enough to hold all valid byte values, so an automatic type conversion takes place.

public class Main {
  public static void main(String[] argv) {
    byte b = 10;/*from   ja v  a  2  s .c  o m*/
    int i = 0;

    i = b;
    System.out.println("b is " + b);
    System.out.println("i is " + i);
  }
}

The output:

For widening conversions, integer and floating-point types are compatible with each other.

public class Main {
  public static void main(String[] argv) {
    int i = 1234;
    float f;/*from   ja va 2s  .c  o  m*/

    f = i;

    System.out.println("i is " + i);
    System.out.println("f is " + f);
  }
}

The output:

Automatic type conversion and type size

Java performs an automatic type conversion when storing a literal integer constant into variables of type byte, short, or long.

public class Main {
  public static void main(String[] argv) {
    byte b = 1;
  }
}

But you cannot store a value out of the byte scope

public class Main{
   public static void main(String[] argv){
      byte b = 11111;
    }
}

When compiling, it generates the following error message:

Next chapter...

What you will learn in the next chapter:

  1. Compiler error for not compatible type
  2. How to cast my type to another type
Home » Java Tutorial » Primitive Data Types

Introduction

    Java Primitive Data Types

Boolean

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

Char

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

Byte

    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

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

Integer

    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

Long

    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

Float

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

Double

    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