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;//from j ava  2  s . 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  v  a 2  s. c om*/
    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 ja  va2 s .  c  om*/
    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 {
/* ja v  a  2s .  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);
  }//  j  av a  2s .c om
}

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);
  }//from   j  a v a  2 s  .co  m
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to declare Java char type
  2. How to create char Literals
  3. How to use escape sequences and create special characters
  4. How to create a Character object from char value
Home » Java Tutorial » Data Types

Java Primitive Data Types
Java byte type
Java byte type conversion
Java short type
Java short type conversion
Java int type
Java int type conversion
Java long type
Java long type conversion
Java float type
Java float type conversion
Java double type
Java double type creation and comparison
Java double type conversion
Java Automatic Type Conversion and Casting
Data type casting
Java type promotion
Java char type
Java char conversion
Java char value and its attributes
Java boolean type
Java boolean type conversion
Autoboxing and auto-unboxing
Java Array
Create an Array
Array Index and length
Multidimensional Arrays
Array examples
Array copy
Array compare
Array Binary search
Array sort
Array to List
Array fill value
Array to String
BigInteger class
BigInteger creation
BigInteger add, subtract, multiply and divide
BigInteger power and modPow
BigInteger conversion
BigInteger to String
BigInteger bit and,or,xor,test,flip,negate
BigInteger bit shift left and right
BigInteger prime value
BigDecimal
BigDecimal constants
BigDecimal Rounding mode
BigDecimal creation
BigDecimal calculation
BigDecimal convert
BigDecimal Comparison
BigDecimal to String
BigDecimal decimal point
BigDecimal precision
BigDecimal format
Currency class