Assignment contexts allow the value of an expression to be assigned (§15.26) to a variable; the type of the expression must be converted to the type of the variable.
Assignment contexts allow the use of one of the following:
- an identity conversion (§5.1.1)
- a widening primitive conversion (§5.1.2)
- a widening reference conversion (§5.1.5)
- a widening reference conversion followed by an unboxing conversion
- a widening reference conversion followed by an unboxing conversion, then followed by a widening primitive conversion
- a boxing conversion (§5.1.7)
- a boxing conversion followed by a widening reference conversion
- an unboxing conversion (§5.1.8)
- an unboxing conversion followed by a widening primitive conversion
If, after the conversions listed above have been applied, the resulting type is a raw type (§4.8), an unchecked conversion (§5.1.9) may then be applied.
In addition, if the expression is a constant expression (§15.29) of type byte, short, char, or int:
- A narrowing primitive conversion may be used if the variable is of type
byte,short, orchar, and the value of the constant expression is representable in the type of the variable. - A narrowing primitive conversion followed by a boxing conversion may be used if the variable is of type
Byte,Short, orCharacter, and the value of the constant expression is representable in the typebyte,short, orcharrespectively.
The compile-time narrowing of constant expressions means that code such as:
byte theAnswer = 42;
is allowed. Without the narrowing, the fact that the integer literal 42 has type int would mean that a cast to byte would be required:
byte theAnswer = (byte)42; // cast is permitted but not required
Finally, a value of the null type (the null reference is the only such value) may be assigned to any reference type, resulting in a null reference of that type.
It is a compile-time error if the chain of conversions contains two parameterized types that are not in the subtype relation (§4.10).
An example of such an illegal chain would be:
Integer, Comparable<Integer>, Comparable, Comparable<String>
The first three elements of the chain are related by widening reference conversion, while the last entry is derived from its predecessor by unchecked conversion. However, this is not a valid assignment conversion, because the chain contains two parameterized types, Comparable<Integer> and Comparable<String>, that are not subtypes.
If the type of an expression can be converted to the type of a variable by assignment conversion, we say the expression (or its value) is assignable to the variable or, equivalently, that the type of the expression is assignment compatible with the type of the variable.
If the type of the variable is float or double, then value set conversion (§5.1.13) is applied to the value v that is the result of the conversion(s):
- If
vis of typefloatand is an element of the float-extended-exponent value set, then the implementation must mapvto the nearest element of the float value set. This conversion may result in overflow or underflow. - If
vis of typedoubleand is an element of the double-extended-exponent value set, then the implementation must mapvto the nearest element of the double value set. This conversion may result in overflow or underflow.
The only exceptions that may arise from conversions in an assignment context are:
-
A
ClassCastExceptionif, after the conversions above have been applied, the resulting value is an object which is not an instance of a subclass or subinterface of the erasure (§4.6) of the type of the variable.This circumstance can only arise as a result of heap pollution (§4.12.2). In practice, implementations need only perform casts when accessing a field or method of an object of parameterized type when the erased type of the field, or the erased return type of the method, differ from its unerased type.
-
An
OutOfMemoryErroras a result of a boxing conversion. -
A
NullPointerExceptionas a result of an unboxing conversion on a null reference. -
An
ArrayStoreExceptionin special cases involving array elements or field access (§10.5, §15.26.1).
Example 5.2-1. Assignment for Primitive Types
class Test {
public static void main(String[] args) {
short s = 12; // narrow 12 to short
float f = s; // widen short to float
System.out.println("f=" + f);
char c = '\u0123';
long l = c; // widen char to long
System.out.println("l=0x" + Long.toString(l,16));
f = 1.23f;
double d = f; // widen float to double
System.out.println("d=" + d);
}
}
This program produces the output:
f=12.0
l=0x123
d=1.2300000190734863
The following program, however, produces compile-time errors:
class Test {
public static void main(String[] args) {
short s = 123;
char c = s; // error: would require cast
s = c; // error: would require cast
}
}
because not all short values are char values, and neither are all char values short values.
Example 5.2-2. Assignment for Reference Types
class Point { int x, y; }
class Point3D extends Point { int z; }
interface Colorable { void setColor(int color); }
class ColoredPoint extends Point implements Colorable {
int color;
public void setColor(int color) { this.color = color; }
}
class Test {
public static void main(String[] args) {
// Assignments to variables of class type:
Point p = new Point();
p = new Point3D();
// OK because Point3D is a subclass of Point
Point3D p3d = p;
// Error: will require a cast because a Point
// might not be a Point3D (even though it is,
// dynamically, in this example.)
// Assignments to variables of type Object:
Object o = p; // OK: any object to Object
int[] a = new int[3];
Object o2 = a; // OK: an array to Object
// Assignments to variables of interface type:
ColoredPoint cp = new ColoredPoint();
Colorable c = cp;
// OK: ColoredPoint implements Colorable
// Assignments to variables of array type:
byte[] b = new byte[4];
a = b;
// Error: these are not arrays of the same primitive type
Point3D[] p3da = new Point3D[3];
Point[] pa = p3da;
// OK: since we can assign a Point3D to a Point
p3da = pa;
// Error: (cast needed) since a Point
// can't be assigned to a Point3D
}
}
The following test program illustrates assignment conversions on reference values, but fails to compile, as described in its comments. This example should be compared to the preceding one.
class Point { int x, y; }
interface Colorable { void setColor(int color); }
class ColoredPoint extends Point implements Colorable {
int color;
public void setColor(int color) { this.color = color; }
}
class Test {
public static void main(String[] args) {
Point p = new Point();
ColoredPoint cp = new ColoredPoint();
// Okay because ColoredPoint is a subclass of Point:
p = cp;
// Okay because ColoredPoint implements Colorable:
Colorable c = cp;
// The following cause compile-time errors because
// we cannot be sure they will succeed, depending on
// the run-time type of p; a run-time check will be
// necessary for the needed narrowing conversion and
// must be indicated by including a cast:
cp = p; // p might be neither a ColoredPoint
// nor a subclass of ColoredPoint
c = p; // p might not implement Colorable
}
}
Example 5.2-3. Assignment for Array Types
class Point { int x, y; }
class ColoredPoint extends Point { int color; }
class Test {
public static void main(String[] args) {
long[] veclong = new long[100];
Object o = veclong; // okay
Long l = veclong; // compile-time error
short[] vecshort = veclong; // compile-time error
Point[] pvec = new Point[100];
ColoredPoint[] cpvec = new ColoredPoint[100];
pvec = cpvec; // okay
pvec[0] = new Point(); // okay at compile time,
// but would throw an
// exception at run time
cpvec = pvec; // compile-time error
}
}
In this example:
-
The value of
veclongcannot be assigned to aLongvariable, becauseLongis a class type other thanObject. An array can be assigned only to a variable of a compatible array type, or to a variable of typeObject,Cloneableorjava.io.Serializable. -
The value of
veclongcannot be assigned tovecshort, because they are arrays of primitive type, andshortandlongare not the same primitive type. -
The value of
cpveccan be assigned topvec, because any reference that could be the value of an expression of typeColoredPointcan be the value of a variable of typePoint. The subsequent assignment of the newPointto a component ofpvecthen would throw anArrayStoreException(if the program were otherwise corrected so that it could be compiled), because aColoredPointarray cannot have an instance ofPointas the value of a component. -
The value of
pveccannot be assigned tocpvec, because not every reference that could be the value of an expression of typePointcan correctly be the value of a variable of typeColoredPoint. If the value ofpvecat run time were a reference to an instance ofPoint[], and the assignment tocpvecwere allowed, a simple reference to a component ofcpvec, say,cpvec[0], could return aPoint, and aPointis not aColoredPoint. Thus to allow such an assignment would allow a violation of the type system. A cast may be used (§5.5, §15.16) to ensure thatpvecreferences aColoredPoint[]:cpvec = (ColoredPoint[])pvec; // OK, but may throw an // exception at run time