1. Technology

The String Class

By

Every character you type on a computer, whether it's a letter, number, or symbol, is represented by a Unicode code point. Unicode is a standard way of getting a computer to store and retrieve the text we write by using numbered codes. When we look at a word like "normal" we see the letters and read them as a word. In Java, the word "normal" is viewed as a sequence of Unicode characters. This sequence of characters is known as a String.

The String Class

It's easy to think that the text we use in a Java application should be a primitive data type. After all, making some characters into words is easy for us. However for computers it's a little more tricky because they like numbers a lot more than letters.

For the Java language to allow us to do all the things we want to do with Strings, they need to be stored as objects. To make things easy for Java programmers there is a String class that is used to create and manipulate these character sequences.

There are several ways to create a String object. The most simplest is to use a String literal. A String literal is a series of letters, numbers or symbols contained within two quotation marks:

"I am a String literal"

When the Java compiler sees a String literal it understands that it is a series of Unicode characters. To create a String object all we need to do is assign a String object to the String literal:

String text = "I am a String literal";

One thing to note about String objects is that once they have been assigned a value it cannot be changed. In Java speak, we say Strings are immutable. Immutable just means that the data contained within an object cannot be edited - it always remains the same. You could write some Java code thinking that you have changed the value of the String object called text to be all lowercase letters:

String text = "I am a String literal";
text = text.toLowerCase();

And, the sequence of character contained within the text object will now be all lowercase but it's not the same String object. Behind the scenes the manipulation of the String value is performed and a new String object for text is created.

You can also create a String from an array of chars:

char characterArray[] = {'C', 'h', 'a','r','a', 'c', 't','e','r','s'};
String characterString = new String(characterArray);

or even an array of byte values:

byte byteArray[] = {67,104,97,114,97,99,116,101,114,115};
String byteString = new String(byteArray);

Each byte corresponds to a Unicode number for each letter.

If you look at the String class you'll see there are thirteen constructors that you can use to create a new String. The easiest way is to use String literals.

If you want to create an empty string you can use an empty String literal:

String emptyString = "";

or just create a new String with no value:

String emptyString = new String();

The main thing to remember is a String is not a primitive data type but actually an object.

Example Java code can be found in the Fun With Strings Example Code.

  1. About.com
  2. Technology
  3. Java
  4. Java Syntax
  5. Understanding Data Types
  6. Java Syntax: The String Class

©2014 About.com. All rights reserved.