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 method invocation:
- If T is
boolean, then useBoolean.valueOf(x). - If T is
char, then useCharacter.valueOf(x). - If T is
byte,short, orint, then useInteger.valueOf(x). - If T is
long, then useLong.valueOf(x). - If T is
float, then useFloat.valueOf(x). - If T is
double, then useDouble.valueOf(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.