Java type promotion

In this chapter you will learn:

  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

Automatic Type Promotion in Expressions

For example, examine the following expression:

public class Main {
  public static void main(String[] argv) {
    byte a = 40;// j  a v  a  2s .c o m
    byte b = 50;
    byte c = 100;
    int d = a * b / c;
  }
}

The result of a * b exceeds the range of byte. To handle this kind of problem, Java automatically promotes each byte or short operand to int. a * b is performed using integers.

Automatic promotions and compile-time errors

public class Main {
  public static void main(String[] argv) {
    byte b = 5;/* ja  va  2  s.co m*/
    b = b * 2; // Error! Cannot assign an int to a byte!
  }
}

Compiling the code above generates the following errors:

If you understand the consequences of overflow, use an explicit cast.

public class Main {
  public static void main(String[] argv) {
    byte b = 50;/*from java  2 s  .c o m*/
    b = (byte) (b * 2);

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

The output from the code above is:

Type Promotion Rules

Widening conversions do not lose information about the magnitude of a value. For example, an int value is assigned to a double variable. This conversion is legal because doubles are wider than ints. Java's widening conversions are

  • From a byte to a short, an int, a long, a float, or a double
  • From a short to an int, a long, a float, or a double
  • From a char to an int, a long, a float, or a double
  • From an int to a long, a float, or a double
  • From a long to a float or a double
  • From a float to a double

Widening conversions:

char->int
byte->short->int->long->float->double

Here are the Type Promotion Rules:

  1. All byte and short values are promoted to int.
  2. If one operand is a long, the whole expression is promoted to long.
  3. If one operand is a float, the entire expression is promoted to float.
  4. If any of the operands is double, the result is double.

In the following code, f * b, b is promoted to a float and the result of the subexpression is float.

public class Main {
/* j a va2s .  co m*/
  public static void main(String args[]) {
    byte b = 4;
    float f = 5.5f;
    float result = (f * b);
    System.out.println("f * b = " + result);
  }
}

The output:

In the following program, c is promoted to int, and the result is of type int.

public class Main {
  public static void main(String args[]) {
    char c = 'a';
    int i = 50000;
    int result = i / c;
    System.out.println("i / c is " + result);
  }/*  ja v  a2 s  .c o  m*/
}

The output:

In the following code the value of s is promoted to double, and the type of the subexpression is double.

public class Main {
  public static void main(String args[]) {
    short s = 1024;
    double d = .1234;
    double result = d * s;
    System.out.println("d * s is " + result);
  }//java 2 s . c om
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. Java Type Wrappers
  2. What are auto-boxing and auto-unboxing
  3. How to use methods and auto-boxing
  4. How do expressions deal with autoboxing/unboxing
  5. Autoboxing/Unboxing Boolean and Character Values
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