The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Object Basics and Simple Data Objects

Strings and the Java Compiler

The Java compiler uses the String and StringBuffer classes behind the scenes to handle literal strings and concatenation.

Literal Strings

In Java, you specify literal strings between double quotes:
"Hello World!"
You can use literal strings anywhere you would use a String object. For example, System.out.println accepts a String argument, so you could use a literal string in place of a String there.
System.out.println("Might I add that you look lovely today.");
You can also use String methods directly from a literal string.
int len = "Goodbye Cruel World".length();
Because the compiler automatically creates a new String object for every literal string it encounters, you can use a literal string to initialize a String.
String s = "Hola Mundo";
The above construct is equivalent to, but more efficient than, this one, which ends up creating two Strings instead of one:
String s = new String("Hola Mundo");
The compiler creates the first string when it encounters the literal string "Hola Mundo!", and the second one when it encounters new String.

Concatenation and the + Operator

In the Java programming language, you can use + to concatenate Strings together:
String cat = "cat";
System.out.println("con" + cat + "enation");
This is a little deceptive because, as you know, Strings can't be changed. However, behind the scenes the compiler uses StringBuffers to implement concatenation. The above example compiles to:
String cat = "cat";
System.out.println(new StringBuffer().append("con").
                       append(cat).append("enation").toString());
You can also use the + operator to append values to a String that are not themselves Strings:
System.out.println("Java's Number " + 1);
The compiler converts the non-String value (the integer 1 in the example) to a String object before performing the concatenation operation.

Note to C and C++ Programmers: The shortcut assignment operator += when used with Strings may confuse C and C++ programmers at first. Recall that a += b is equivalent to a = a + b. Let's look at two code samples written in C++ and the Java programming lanaguage:

//C++ code
string*  s1 = new string("hello");
string*  s2 = s1;
(*s1) += " world";
cout<<*s1<<endl<<*s2<<endl;
return  0;
//s1 = s2 = "hello world"
//Java progamming language code
String s1 = "hello";
String s2 = s1;
s1 += " world";
System.out.println(s1 + "\n" + s2);
//s1 = "hello world" and  s2 = "hello"
In the C++ example, the strings s1 and s2 print the same result because they both point to the same address. In the Java programming language, Strings can't be modified, so the + operator must create a new String when " world" is appended to s1.

The following code sample illustrates that s1 and s2 point to the same object until you use the += operator to assign a new String to s1.

//Java programming language code
String s1 = "hello";
String s2 = s1;
System.out.println("s1 = " + s1
                   + "; s2 = " + s2);
System.out.println("System.identityHashCode(s1) = "
                   + System.identityHashCode(s1));
System.out.println("System.identityHashCode(s2) = "
                   + System.identityHashCode(s2));

s1 += " world";
System.out.println("\ns1 = " + s1
                   + "; s2 = " + s2);
System.out.println("System.identityHashCode(s1) = "
                   + System.identityHashCode(s1));
System.out.println("System.identityHashCode(s2) = "
                   + System.identityHashCode(s2));
Here's the output:
s1 = hello; s2 = hello
System.identityHashCode(s1) = 2452092
System.identityHashCode(s2) = 2452092

s1 = hello world; s2 = hello
System.identityHashCode(s1) = 7474923
System.identityHashCode(s2) = 2452092
s1 points to a new address after " world" is appended.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.