Home : Course Map : Chapter 2 : Java :
Casts & Mixing Primitive Types
Course Map
Chapter 2


Primitive Types
Casts & Mixing
Console Output 

Conditional: if-else
Flow Control

Java vs C/C++
JVM Instructions 1

     About JavaTech
     Codes List
     Topic Index
     Course Guide
     What's New

Converting one type of data into another must follow the rules of casting. If a conversion results in the loss of precision, as in an int value converted to a short, then the compiler will issue an error message unless an explicit cast is made.

To convert type AA data into type BB data, put the type BB name in parentheses in front of the type AA data:

    AA a = aData;
    BB b = (BB)a; // cast type AA to type BB

For example, to convert integer data to floating point:

    int i=0;
    float f;
    f=(float)i; // Cast int as float

Expressions can promote to a wider type without an explicit cast:

    int i=1;  
    long j=3L; // Literals are int types so require L suffix
    j=i;       // OK

However, you can not assign a value to a more narrow type without an explicit cast:

    i=j;       // Error in assigning long to int
    i=(int)j;  // OK

So a data type with lower precision (fewer bits) can be converted to a type of higher precision without explicit casting. To convert a higher precision type to a lower precision, however, an explicit cast is required or the compiler will flag an error.

Note that when you cast a value of a wider type down to a more narrow type, such as an int value to a byte variable, the upper bytes will be truncated. That is, the lowest order byte in the int value will be copied to the byte value.

Primitive Type Conversion Table

Below is a table that indicates to which of the other primitive types you can cast a given primitive data type. The symbol C indicates that an explicit cast is required since the precision is decreasing. The symbol A indicates that the precision is increasing so an automatic cast occurs without the need for an explicit cast. N indicates that the conversion is not allowed.

  int long float double char byte short boolean
int - A A* A C C C N
long C - A* A* C C C N
float C C - A C C C N
double C C C - C C C N
char A A A A - C C N
byte A A A A C - A N
short A A A A C C - N
boolean N N N N N N N -

The * asterisk indicates that the least significant digits may be lost in the conversion even though the target type allows for bigger numbers. For example, a large value in an int type value that uses all 32 bits will lose some of the lower bits when converted to float since the exponent uses 8 bits of the 32 provided for float values.

Mixed Types in an Expression

If an expression holds a mix of types, the lower precision or narrower value operand is converted to a higher precision or wider type. This result then must be cast if it goes to a lower precision type:

    float x,y=3;
    x= i*y;       // OK since i will be promoted to float
    j= i*y;       //
Error since result is a float value
    j= (int)(i*y) // OK

The process of converting a value to a wider or higher precision integer or floating point type is called "numeric promotion". The Java VM specification states the following rules for promotion in an expression of two operands, as in x+i:

  • If either operand is of type double, the other is converted to double.
  • Otherwise, if either operand is of type float, the other is converted to float.
  • Otherwise, if either operand is of type long, the other is converted to long.
  • Otherwise, both operands are converted to type int.

Chapter 2: Tech section adds more discussion about casting.


Latest update: Oct. 14, 2004

Arithmetic Ops
Math Class
More on Integers
FP : Overview
FP : Java  
Demo 1
More Mix/Cast
  Demo 2

Differential Eq.
Euler Method
Demo 1
Demo 2

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
22 23 24

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.