---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 14 Edition"
java_version: 14
chapter: "Chapter 5. Conversions and Contexts"
section: "5.1.11"
heading: "5.1.11. String Conversion"
parent: "5.1. Kinds of Conversion"
source: https://docs.oracle.com/javase/specs/jls/se14/html/jls-5.html#jls-5.1.11
id: "5.1.11"
url: https://jvm.obfuscated.software/jls/se14/5.1.11.html
raw: https://jvm.obfuscated.software/jls/se14/5.1.11.md
---

# 5.1.11. String Conversion

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](15.9.md)):

- 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.

Now 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`).

- 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` ([§4.3.2](4.3.2.md)). Many classes override it, notably `Boolean`, `Character`, `Integer`, `Long`, `Float`, `Double`, and `String`.
