Any type may be converted to type String by string conversion.
A value x of primitive type T is first converted to a reference value as if by giving it as an argument to an appropriate class instance creation expression (§15.9):
- If T is
boolean, then usenew Boolean(x). - If T is
char, then usenew Character(x). - If T is
byte,short, orint, then usenew Integer(x). - If T is
long, then usenew Long(x). - If T is
float, then usenew Float(x). - If T is
double, then usenew Double(x).
This reference value is then converted to type String by string conversion.
Now only reference values need to be considered:
-
If the reference is
null, it is converted to the string "null" (four ASCII charactersn,u,l,l). -
Otherwise, the conversion is performed as if by an invocation of the
toStringmethod of the referenced object with no arguments; but if the result of invoking thetoStringmethod isnull, then the string "null" is used instead.The
toStringmethod is defined by the primordial classObject(§4.3.2). Many classes override it, notablyBoolean,Character,Integer,Long,Float,Double, andString.