SE450: Types: Coercion [36/47] Previous pageContentsNext page

Coercion is very different from casting.

Java uses the same syntax for both concepts.

Coercion for base types. Casting for Object types.

Coercion changes the actual type of the object -- Casting changes only the declared type.

file:types/coercion/Main.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package types.coercion;
class Main {
  private Main() {}
  @SuppressWarnings("cast")
  public static void main(String[] args) {
    int i0 = 1;
    float f0 = i0;         // compiles ok, runs ok (implicit up-coercion)
    float f1 = (float) i0; // compiles ok, runs ok (explicit up-coercion)

    float f2 = 1.42f;
    //  int i2 = f2;       // compiler error (implicit down-coercion)
    int i3 = (int) f2; // compiles ok, runs ok (explicit down-coercion)

    // Note that coercion changes the actual value, not just the
    // declared type.
    float f3 = i3;
    System.out.println("f2=" + f2);
    System.out.println("f3=" + f3);
  }
}
class CoercionOrdering {
  private CoercionOrdering() {}
  public static void showingCoercionOrder() {
    // booleans and object types do not coerce
    // use ?: to convert booleans to other types
    // use ?: also for object types
    boolean p = false;
    int j = p ? 1 : 0;
    Object o = null;
    int k = (null==o) ? 0 : 1;

    // char and short are both 16 bit, but mutually incomparable
    char  c = '\0';
    short z = 0;
    z = (short) c;
    c = (char) z;

    // number types from bottom to top
    byte   b = 0;
    short  s = b;
    int    i = s;
    long   l = i;
    float  f = l;
    double d = f;

    // number types from top to bottom, losing precision
    f = (float) d;
    l = (long) f;
    i = (int) l;
    s = (short) i;
    b = (byte) s;
  }
}

Previous pageContentsNext page