String literals

The String class represents character strings in Java programs. We have seen several such strings. We have seen Hello, world!, for example. That is a String literal.

Another example of a literal is null. Any reference class can refer to a literal null. It represents a reference value that does not point to any object. In the case of a String type, it appears as follows:

String s = null;

But a literal that consists of characters enclosed in double quotes ("abc""123""a42%$#") can only be of a String type. In this respect, the String class, being a reference type, has something in common with primitive types. All String literals are stored in a dedicated section of memory called the string pool and two literals equally spelled to represent the same value from the pool:

String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2); //prints: true
System.out.println("abc" == s1); //prints: true

The JVM authors have chosen such an implementation to avoid duplication and improve memory usage. The previous code examples look very much like operations with primitive types, don't they? But when a String object is created using a new operator, the memory for the new object is allocated outside the string pool, so references of two String objects, or any other objects for that matter, are always different:

String o1 = new String("abc");
String o2 = new String("abc");
System.out.println(o1 == o2); //prints: false
System.out.println("abc" == o1); //prints: false

If necessary, it is possible to move the string value created with the new operator to the string pool using the intern() method:

String o1 = new String("abc");
System.out.println("abc" == o1); //prints: false
System.out.println("abc" == o1.intern()); //prints: true

In the previous code, the intern() method attempted to move the newly created "abc" value into the string pool but discovered that such a literal exists there already, so it reused the literal from the string pool. That is why the references in the last line in the preceding example are equal.

The good news is that you probably will not need to create String objects using the new operator, and most Java programmers never do it. But when a String object is passed into your code as an input and you have no control over its origin, comparison by reference only may cause an incorrect result (if the strings have the same spelling but were created by the new operator). That is why, when the equality of two strings by spelling (and case) is necessary, in order to compare two literals or String objects, the equals() method is a better choice:

String o1 = new String("abc");
String o2 = new String("abc");
System.out.println(o1.equals(o2)); //prints: true
System.out.println(o2.equals(o1)); //prints: true
System.out.println(o1.equals("abc")); //prints: true
System.out.println("abc".equals(o1)); //prints: true
System.out.println("abc".equals("abc")); //prints: true

We will talk about the equals() and other methods of the String class shortly.

Another feature that makes String literals and objects look like primitive values is that they can be added using the arithmetic operator, +:

String s1 = "abc";
String s2 = "abc";
String s = s1 + s2;
System.out.println(s); //prints: abcabc
System.out.println(s1 + "abc"); //prints: abcabc
System.out.println("abc" + "abc"); //prints: abcabc

String o1 = new String("abc");
String o2 = new String("abc");
String o = o1 + o2;
System.out.println(o); //prints: abcabc
System.out.println(o1 + "abc"); //prints: abcabc

No other arithmetic operator can be applied to a String literal or an object.

And, finally, a new String literal, called a raw string literal, was introduced with Java 12. It allows preserving indentation and multiple lines without adding white spaces in quotes. For example, here is how a programmer would add the indentation before Java 12 and use \n to break the line:

String html = "<html>\n" +
              "    <body>\n" +
              "             <p>Hello World.</p>\n" +
              "    </body>\n" +
              "</html>\n";

And here is how the same result is achieved with Java 12:

String html = `<html>
<body>
<p>Hello World.</p>
</body>
</html>
`;

As you can see, a raw string literal consists of one or more characters enclosed in a backtick (`) (\u0060), also called a backquote or an accent grave.