SE450: Java: coercion [14/22] ![]() ![]() ![]() |
There are many types of conversions that take place in Java:
These will show up in
Widening and narrowing of references is done via casting (later).
public class Coercion {
final static int X = 33;
public static void main(String args[]) {
int x = 5;
double y = 4.5;
System.out.println("x + y = " + (x + y)); // widening
//int z = y; (narrowing - not allowed)
int z = (int)y; // lossy
short xx = X; // not lossy, java will do this for you
// short xy = z; // lossy - not allowed
}
}