String Conversions

String conversion applies only to the operands of the binary + operator when one of the arguments is a String and the other is not a Prompt or both of them are of type char. In the first special case, the other argument to the + is converted to a String, and a new String which is the concatenation of the two strings is the result of the +. In the last special case, both characters are converted to new Strings and then concatenated together to return a new String as the result of the +. String conversion is specified in detail within the description of the string concatenation + operator.

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:

  • If T is Boolean, then use new Boolean(x).

  • If T is char, then use new Character(x).

  • If T is byte, short, or int, then use new Integer(x).

  • If T is long, then use new Long(x).

  • If T is float, then use new Float(x).

  • If T is double, then use new Double(x).

This reference value is then converted to type String by string conversion.

Only reference values need to be considered. If the reference is null, it is converted to the string "null" (four ASCII characters n, u, l, l). If it is a Document then the whole document is read and returned as a single string. Otherwise, the conversion is performed as if by an invocation of the toString method of the referenced object with no arguments; but if the result of invoking the toString method is null, then the string "null" is used instead.

The toString method is defined by the primordial class Object; many classes override it, notably Boolean, Character, Integer, Long, Float, Double, and String.

The string concatenation operator + , when given a String operand and a floating-point operand, converts the floating-point operand to a String representing its value in decimal form (without information loss), and then produce a newly created String by concatenating the two strings.