|
| 1 | +[[painless-casting]] |
| 2 | +=== Casting |
| 3 | + |
| 4 | +Casting is the conversion of one type to another. Implicit casts are casts that |
| 5 | +occur automatically, such as during an assignment operation. Explicit casts are |
| 6 | +casts where you use the casting operator to explicitly convert one type to |
| 7 | +another. This is necessary during operations where the cast cannot be inferred. |
| 8 | + |
| 9 | +To cast to a new type, precede the expression by the new type enclosed in |
| 10 | +parentheses, for example |
| 11 | +`(int)x`. |
| 12 | + |
| 13 | +The following sections specify the implicit casts that can be performed and the |
| 14 | +explicit casts that are allowed. The only other permitted cast is casting |
| 15 | +a single character `String` to a `char`. |
| 16 | + |
| 17 | +*Grammar:* |
| 18 | +[source,ANTLR4] |
| 19 | +---- |
| 20 | +cast: '(' TYPE ')' expression |
| 21 | +---- |
| 22 | + |
| 23 | +[[numeric-casting]] |
| 24 | +==== Numeric Casting |
| 25 | + |
| 26 | +The following table shows the allowed implicit and explicit casts between |
| 27 | +numeric types. Read the table by row. To find out if you need to explicitly |
| 28 | +cast from type A to type B, find the row for type A and scan across to the |
| 29 | +column for type B. |
| 30 | + |
| 31 | +IMPORTANT: Explicit casts between numeric types can result in some data loss. A |
| 32 | +smaller numeric type cannot necessarily accommodate the value from a larger |
| 33 | +numeric type. You might also lose precision when casting from integer types |
| 34 | +to floating point types. |
| 35 | + |
| 36 | +|==== |
| 37 | +| | byte | short | char | int | long | float | double |
| 38 | +| byte | | implicit | implicit | implicit | implicit | implicit | implicit |
| 39 | +| short | explicit | | explicit | implicit | implicit | implicit | implicit |
| 40 | +| char | explicit | explicit | | implicit | implicit | implicit | implicit |
| 41 | +| int | explicit | explicit | explicit | | implicit | implicit | implicit |
| 42 | +| long | explicit | explicit | explicit | explicit | | implicit | implicit |
| 43 | +| float | explicit | explicit | explicit | explicit | explicit | | implicit |
| 44 | +| double | explicit | explicit | explicit | explicit | explicit | explicit | |
| 45 | +|==== |
| 46 | + |
| 47 | + |
| 48 | +Example(s) |
| 49 | +[source,Java] |
| 50 | +---- |
| 51 | +int a = 1; // Declare int variable a and set it to the literal |
| 52 | + // value 1 |
| 53 | +long b = a; // Declare long variable b and set it to int variable |
| 54 | + // a with an implicit cast to convert from int to long |
| 55 | +short c = (short)b; // Declare short variable c, explicitly cast b to a |
| 56 | + // short, and assign b to c |
| 57 | +byte d = a; // ERROR: Casting an int to a byte requires an explicit |
| 58 | + // cast |
| 59 | +double e = (double)a; // Explicitly cast int variable a to a double and assign |
| 60 | + // it to the double variable e. The explicit cast is |
| 61 | + // allowed, but it is not necessary. |
| 62 | +---- |
| 63 | + |
| 64 | +[[reference-casting]] |
| 65 | +==== Reference Casting |
| 66 | + |
| 67 | +A reference type can be implicitly cast to another reference type as long as |
| 68 | +the type being cast _from_ is a descendant of the type being cast _to_. A |
| 69 | +reference type can be explicitly cast _to_ if the type being cast to is a |
| 70 | +descendant of the type being cast _from_. |
| 71 | + |
| 72 | +*Examples:* |
| 73 | +[source,Java] |
| 74 | +---- |
| 75 | +List x; // Declare List variable x |
| 76 | +ArrayList y = new ArrayList(); // Declare ArrayList variable y and assign it a |
| 77 | + // newly allocated ArrayList [1] |
| 78 | +x = y; // Assign Arraylist y to List x using an |
| 79 | + // implicit cast |
| 80 | +y = (ArrayList)x; // Explicitly cast List x to an ArrayList and |
| 81 | + // assign it to ArrayList y |
| 82 | +x = (List)y; // Set List x to ArrayList y using an explicit |
| 83 | + // cast (the explicit cast is not necessary) |
| 84 | +y = x; // ERROR: List x cannot be implicitly cast to |
| 85 | + // an ArrayList, an explicit cast is required |
| 86 | +Map m = y; // ERROR: Cannot implicitly or explicitly cast [2] |
| 87 | + // an ArrayList to a Map, no relationship |
| 88 | + // exists between the two types. |
| 89 | +---- |
| 90 | +[1] `ArrayList` is a descendant of the `List` type. |
| 91 | +[2] `Map` is unrelated to the `List` and `ArrayList` types. |
| 92 | + |
| 93 | +[[def-type-casting]] |
| 94 | +==== def Type Casting |
| 95 | +All primitive and reference types can always be implicitly cast to |
| 96 | +`def`. While it is possible to explicitly cast to `def`, it is not necessary. |
| 97 | + |
| 98 | +However, it is not always possible to implicitly cast a `def` to other |
| 99 | +primitive and reference types. An explicit cast is required if an explicit |
| 100 | +cast would normally be required between the non-def types. |
| 101 | + |
| 102 | + |
| 103 | +*Examples:* |
| 104 | +[source,Java] |
| 105 | +---- |
| 106 | +def x; // Declare def variable x and set it to null |
| 107 | +x = 3; // Set the def variable x to the literal 3 with an implicit |
| 108 | + // cast from int to def |
| 109 | +double a = x; // Declare double variable a and set it to def variable x, |
| 110 | + // which contains a double |
| 111 | +int b = x; // ERROR: Results in a run-time error because an explicit cast is |
| 112 | + // required to cast from a double to an int |
| 113 | +int c = (int)x; // Declare int variable c, explicitly cast def variable x to an |
| 114 | + // int, and assign x to c |
| 115 | +---- |
| 116 | + |
| 117 | +[[boxing-unboxing]] |
| 118 | +==== Boxing and Unboxing |
| 119 | + |
| 120 | +Boxing is where a cast is used to convert a primitive type to its corresponding |
| 121 | +reference type. Unboxing is the reverse, converting a reference type to the |
| 122 | +corresponding primitive type. |
| 123 | + |
| 124 | +There are two places Painless performs implicit boxing and unboxing: |
| 125 | + |
| 126 | +* When you call methods, Painless automatically boxes and unboxes arguments |
| 127 | +so you can specify either primitive types or their corresponding reference |
| 128 | +types. |
| 129 | +* When you use the `def` type, Painless automatically boxes and unboxes as |
| 130 | +needed when converting to and from `def`. |
| 131 | + |
| 132 | +The casting operator does not support any way to explicitly box a primitive |
| 133 | +type or unbox a reference type. |
| 134 | + |
| 135 | +If a primitive type needs to be converted to a reference type, the Painless |
| 136 | +reference type API supports methods that can do that. However, under normal |
| 137 | +circumstances this should not be necessary. |
| 138 | + |
| 139 | +*Examples:* |
| 140 | +[source,Java] |
| 141 | +---- |
| 142 | +Integer x = 1; // ERROR: not a legal implicit cast |
| 143 | +Integer y = (Integer)1; // ERROR: not a legal explicit cast |
| 144 | +int a = new Integer(1); // ERROR: not a legal implicit cast |
| 145 | +int b = (int)new Integer(1); // ERROR: not a legal explicit cast |
| 146 | +---- |
| 147 | + |
| 148 | +[[promotion]] |
| 149 | +==== Promotion |
| 150 | + |
| 151 | +Promotion is where certain operations require types to be either a minimum |
| 152 | +numerical type or for two (or more) types to be equivalent. |
| 153 | +The documentation for each operation that has these requirements |
| 154 | +includes promotion tables that describe how this is handled. |
| 155 | + |
| 156 | +When an operation promotes a type or types, the resultant type |
| 157 | +of the operation is the promoted type. Types can be promoted to def |
| 158 | +at compile-time; however, at run-time, the resultant type will be the |
| 159 | +promotion of the types the `def` is representing. |
| 160 | + |
| 161 | +*Examples:* |
| 162 | +[source,Java] |
| 163 | +---- |
| 164 | +2 + 2.0 // Add the literal int 2 and the literal double 2.0. The literal |
| 165 | + // 2 is promoted to a double and the resulting value is a double. |
| 166 | +
|
| 167 | +def x = 1; // Declare def variable x and set it to the literal int 1 through |
| 168 | + // an implicit cast |
| 169 | +x + 2.0F // Add def variable x and the literal float 2.0. |
| 170 | + // At compile-time the types are promoted to def. |
| 171 | + // At run-time the types are promoted to float. |
| 172 | +---- |
0 commit comments