Java type promotion
In this chapter you will learn:
- What is Automatic Type Promotion in Expressions
- How to fix the compile-time errors caused by automatic type promotions
- 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:
- All
byte
andshort
values are promoted toint
. - If one operand is a
long
, the whole expression is promoted tolong
. - If one operand is a
float
, the entire expression is promoted tofloat
. - If any of the operands is
double
, the result isdouble
.
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:
- Java Type Wrappers
- What are auto-boxing and auto-unboxing
- How to use methods and auto-boxing
- How do expressions deal with autoboxing/unboxing
- Autoboxing/Unboxing Boolean and Character Values