Java Fundamentals  «Prev  Next»
Lesson 6Manipulating text with strings
ObjectiveUse strings to represent text in Java.

Manipulating Text with Strings in Java

Unlike the other data types you have learned about, strings of text in Java are represented by a special class[1] named String.
The String class provides all kinds of neat features for manipulating strings in Java.
Following is an example of creating a String object:

Java, Strings and the C++ Programming Language

If you are coming to Java from the C or C++ Programming language, let me clarify a very important point: a string in Java is not an array of characters.
In C/C++ there is no such thing as a string data type since a C/C++ "string" is just a character array. Java strings use the String class, and are therefore created as objects. This results in Java strings being much more powerful and easy to use than C/C++ "strings."

Strings Are Immutable Objects

We will start with a little background information about strings. You may not need this for the Java test, but a little context will help. You know what I always say:
Within the context of the Java Programming Language.

Handling "strings" of characters is a fundamental aspect of most programming languages. In Java, each character in a string is a 16-bit Unicode character. Because Unicode characters are 16 bits (not 7 or 8 bits that ASCII provides), a rich, international set of characters is easily represented in Unicode.

In Java, strings are objects. As with other objects, you can create an instance of a string with the new keyword, as follows:
String s = new String();

This line of code creates a new object of class String and assigns it to the reference variable s.
So far, String objects seem just like other objects. Now, let us give the string a value:
s = "abcdef";

These two lines of code are not quite what they seem.
It turns out that the String class has multiple constructors, so you can use a more efficient shortcut:
String s = new String("abcdef");


And this is even more concise:
String s = "abcdef";
There are some subtle differences between these options that we will discuss later, but what they have in common is that they all create a new String object, with a value of "abcdef", and assign it to a reference variable s. Now let us say that you want a second reference to the String object referred to by s:
String s2 = s; // refer s2 to the same String as s
Once you have assigned a String a value, that value can never change because it is immutable or frozen solid.

String emotion;

As this code shows, creating a string is very much like creating a variable of any other Java data type. However, this code only creates a placeholder for a string; memory is not allocated for the string until you use the new operator, like this:

String emotion = new String();

The String object in this code is created and ready for use. However, it does not hold any text yet. Assigning a value to a String object is very easy, as the following code demonstrates:
emotion = "happy";

You can also initialize a string when you declare it, like this:

String emotion = new String("happy"); 

You can even shorten the initialization code to the following:
String emotion = "happy"; 

The new operator is not necessary in this example because memory has already been allocated for the string literal "happy". This is due to the fact that String literals are themselves objects. This also means that you can assign a string literal to a string variable that has not been created via the new operator.
[1]Class: A class is a template that defines the implementation of an object, effectively acting as a blueprint for the object. A class defines data and methods and is a unit of organization in a Java program.