Java String Class

The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.

Every string you create is actually an object of type String. Even string constants are actually String objects. For example, in the statement

System.out.println("This is a String, too");

the string ―"This is a String, too" is a String constant.


Creating String Object

In Java, strings are objects. Strings can be constructed in a variety of ways.

First way: Just like 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. Now, let's give the String a value:

s = "abcdef";

We can combine these two statements into one as:

String s = new String("abcdef");

Second way: The easiest is to use a statement like this:

access_specifier String string_name = "string";


private String myString = "this is a test";

Once you have created a String object, you can use it anywhere that a string is allowed. For example, this statement displays myString:


String can be created by concatenating String objects with +. It is used to concatenate two strings. For example, this statement

String myString = "I" + " like " + "Java.";

results in myString containing ―I like Java.

And there are many other ways to create a String objects which we'll look later.

The following program explains more about creation of String object.

 * Demonstrating Strings.
class StringDemo {
    public static void main(String args[]) {
        String strOb1 = "First String";
        String strOb2 = "Second String";
        String strOb3 = strOb1 + " and " + strOb2;

The output of the program is:

First String
Second String
First String and Second String


Strings Are Immutable Objects

Strings are constant i.e. objects of type String are immutable; their values cannot be changed after they are created. However, you can always create a new one that contains the modifications. Because String objects are immutable they can be shared.

Java defines a peer class of String, called StringBuffer, which allows strings to be altered, so all of the normal string manipulations are still available in Java. StringBuffer supports mutable strings.

Let us create a string object as

String s = new String("abc"); 

So, a new String object, with a value of "abc" is created, and is assigned it to a reference variable s.

Now let's 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

Now, lets create new string and assign it to s.

// create a new String object, with value "abcdef more stuff", refer s to it. 
s = s +"def";

Remember s2 is still referring to the original "abcdef" String.

Here is pictorial representation of memory.


Figure: Immutability of String

For more on String class, please go through SCJP Sun Certified Programmer for Java 6 Exam 310-065. It has explained about strings in more clear and conscise format.


Important Methods in the String Class

The following methods are some of the more commonly used methods in the String class.

  • charAt() Returns the character located at the specified index
  • concat() Appends one String to the end of another ( "+" also works)
  • equalsIgnoreCase() Determines the equality of two Strings, ignoring case
  • length() Returns the number of characters in a String
  • replace() Replaces occurrences of a character with a new character
  • substring() Returns a part of a String
  • toLowerCase() Returns a String with uppercase characters converted
  • toString() Returns the value of a String
  • toUpperCase() Returns a String with lowercase characters converted
  • trim() Removes whitespace from the ends of a String


The following program uses most of these methods.

 * Demonstrating some String methods.
class StringMethods {
    public static void main(String args[]) {
        String strOb1 = "First String";
        String strOb2 = "Second String  ";
        String strOb3 = strOb1;
        System.out.println("Length of strOb2: " + strOb2.length());

        String strOb1Trimmed = strOb2.trim();
        System.out.println("After trimming, Length of strOb2: "+strOb1Trimmed.length());

        System.out.println("Char at index 3 in strOb1: " + strOb1.charAt(3));

        String strOb1Lower = strOb1.toLowerCase();

        String strOb4 = strOb1.concat(strOb2);
        System.out.println("strOb1.toLowerCase() = "+strOb1Lower);
        System.out.println("strOb1.concat(strOb2) = "+strOb4);
        //checking the equality of string objects
        if (strOb1.equals(strOb2))
            System.out.println("strOb1 == strOb2");
            System.out.println("strOb1 != strOb2");
        if (strOb1.equals(strOb3))
            System.out.println("strOb1 == strOb3");
            System.out.println("strOb1 != strOb3");

The output of the program is:

Length of strOb2: 15
After trimming, Length of strOb2: 13
Char at index 3 in strOb1: s
strOb1.toLowerCase() = first string
strOb1.concat(strOb2) = First StringSecond String  
strOb1 != strOb2
strOb1 == strOb3

Here, last two white space characters are removed by trim() method. So, the length of trimmed strOb2 is only 13.

Note: New String objects are created each time with each methods. Like strOb1.toLowerCase() creates new string object since String objects are immutable.