String in Java


Handling character strings in Java is supported through two final classes: String and StringBuffer. The String class implements immutable character strings, which are read-only once the string has been created and initialized, whereas the StringBuffer class implements dynamic character strings. All string literals in Java programs, are implemented as instances of String class. Strings in Java are 16-bit Unicode.

Note: In JDK 1.5+ you can use StringBuilder, which works exactly like StringBuffer, but it is faster and not thread-safe

The easiest way of creating a String object is using a string literal:

String str1 = “I cant be changed once created!”;

A string literal is a reference to a String object. Since a string literal is a reference, it can be manipulated like any other String reference. ie it can be used to invoke methods of String class.

For example,

The Java language provides special support for the string concatenation operator (+), which has been overloaded for Strings objects. String concatenation is implemented through the StringBuffer class and its append method.

For example,

Would be executed as

The Java compiler optimizes handling of string literals. Only one String object is shared by all string having same character sequence. Such strings are said to be interned, meaning that they share a unique String object. The String class maintains a private pool where such strings are interned.

For example,

Would print Equal when run.

Since the String objects are immutable. Any operation performed on one String reference will never have any effect on other references denoting the same object.


String class provides various types of constructors to create String objects. Some of them are,

String ()

Creates a new String object whose content is empty ie “”.

String (String s)

Creates a new String object whose content is same as the String object passed as an argument.

Note: Constructor creates a new string means it does not intern the String. Interned String object reference can be obtained by using intern () method of the String class

String also provides constructors that take byte and char array as argument and returns String object.

String equality

String class overrides the equals () method of the Object class. It compares the content of the two string object and returns the boolean value accordingly.

For example,

The output would be,

Equal 1

Not Equal 2

Equal 3

Note that == compares the references not the actual contents of the String object; Where as equals method compares actual contents of two String objects.

String class also provides another method equalsIgnoreCase () which ignores the case of contents while comparing.

Apart from these methods String class also provides compareTo methods.

int compareTo (String str2)

This method compares two Strings and returns an int value. It returns
value 0, if this string is equal to the string argument
a value less than 0, if this string is less than the string

a value greater than 0, if this string is greater than the
string argument

int compareTo (Object object)

This method behaves exactly like the first method if the argument object is actually a String object; otherwise, it throws a ClassCastException.

String Manipulations

Reading characters from String:

char charAt (index i)

Returns char at specified index. An index ranges from 0 to length () -1.

Searching characters in String

String class provides indexOf method which searches for the specified character inside the string object. This method has been overloaded. If the search is successful, then it returns the index of the char otherwise -1 is returned.

Returns the index of first occurrence of the argument char.

Finds the index of the first occurrence of the argument character in a string, starting at the index specified in the second argument.

Finds the start index of the first occurrence of the substring argument in a String.

Finds the start index of the first occurrence of the substring argument in a String, starting at the index specified in the second argument.

The String class also provides methods to search for a character or string in backward direction. These methods are given below.

Replacing characters in String

The replace method of String can be used to replace all occurrences of the specified character with given character.

Getting substrings

String class provides substring method to extract specified portion of the given String. This method has been overloaded.

Note: A new String object containing the substring is created and returned. The original String will not be affected.

If the index value is not valid, a StringIndexOutOfBoundsException is thrown.


String class provides set of static overloaded valueOf method to convert primitives and object into strings.

Manipulating Character Case

String class provides following methods to manipulate character case in String.

Note: Original String object is returned if none of the characters changed, otherwise new String object is constructed and returned.

Miscellaneous methods

This method removes white space from the front and the end of a String.

Returns length of the String.

This method returns interned String object, if already present in the String pool. Otherwise this String is added into the pool, and then interned reference is returned.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.