String in Java

In Java a string is a sequence of characters. But, unlike many other languages, Java implements strings as an object of type String.

Java strings are immutable

Implementing strings as a built-in objects allows Java to provide a full complement of features that makes string handling in Java very convenient. For example, Java provide various methods to modify a string object like methods to compare two strings, search for a substring, or concatenate two strings etc. However implementing string as an object make the strings immutable. That is, once a String object has been created, you cannot change the characters of that string. Although, you could still perform all types of string operations. But, each time you need an altered version of an existing string, a new String object is created that contains the modifications.

StringBuffer

For cases, where a modifiable string is required, there is a companion class called StringBuffer, whose objects contain strings that can be modified after they are created. Both the classes (String and StringBuffer) are defined in java.lang package.

Quick tip: Although the contents of a String object cannot be changed after it has been created, the variable declared as a String reference can be changed to point at some other String object at any time.

String Constructors

The String class supports several constructors for various use-cases.

Default Constructor 
String s = new String();

Constructor to create string from an array of characters
String (char chars[])

char chars[] = {'a', 'b', 'c'};
String s = new String(chars);
s is initialised as string "abc".

Constructor to construct string from a subrange of an array of characters
String (chars, int startIndex, int numChars)
Here startIndex is the index at which subrange begin and numChars specify the number of characters to use.

char chars[] = {'a', 'b', 'c', 'd'};
String s = new String(chars, 1, 3);
s is initialised as string "bcd".

Constructor to construct string that contains the same characters sequence as another String object
String (String strObject)

char chars[] = {'a', 'b', 'c'};
String s1 = new String(chars);
String s2 = new String(s1);

System.out.println(s1);
System.out.println(s1);

Output
abc
abc
Code
public class JavaStrings {
	
	public static void main (String args[]) {
		
		char chars[] = {'a', 'b', 'c', 'd'};
		String s  = new String(chars);
		System.out.println(s);
		
		String sNew = new String(chars, 1, 3);
		System.out.println(sNew);
		
		String s1 = new String(chars);
		String s2 = new String(s1);
		System.out.println(s1);
		System.out.println(s2);
		
		if (s1 == s2) {
			System.out.println("String s1 and s2 are same");
		} else if (s1.equals(s2)) {
			System.out.println("Contents of String s1 and s2 are same");
		}
		
		String s3 = s2;
		if (s2 == s3) {
			System.out.println("String s2 and s3 are same");
		} 
		if (s2.equals(s3)) {
			System.out.println("Contents of String s2 and s3 are same");
		}
	}

}

Output

abcd
bcd
abcd
abcd
Contents of String s1 and s2 are same
String s2 and s3 are same
Contents of String s2 and s3 are same

Here’s a working example: Ideone

Reference:
Java 2: The Complete Reference

Leave a Reply