Constructors of the String Class

The String class exposes many constructors.

You can create an empty string by calling the default constructor. An empty string basically contains no characters.

Here is an example.

String emtpyString = new String();

The String class implements a constructor which accepts an array of characters. This allows you to create a string from an array of characters.

String(char[] sequence)

Here is an example which demonstrates how you can create strings using arrays.

char[] sequence = { 'F', 'e', 'b', 'r', 'u', 'a', 'r', 'y' };
String string = new String(sequence);

This creates a string that holds the text "February".

You can further specify a range within the character array you want the constructor to copy using this constructor.

String(char[] sequence, int startIndex, int count)

Here, the startIndex indicates the index from where the characters should be copied. It is inclusive, which means the character at the start index is also included. The count parameter indicates the number of characters to copy, beginning the copy at the start index.

The following example demonstrates this constructor.

char[] sequence = { 'F', 'e', 'b', 'r', 'u', 'a', 'r', 'y' };
String string = new String(sequence, 3, 2);

This creates a string which represents “ru”. Remember indexes in Java always begin at 0, therefore, 3 indicates the fourth character.

You can create a copy of the string object using this constructor.

String(String other)

Here is an example.

String string1 = "Hello";
String string2 = new String(string1);
System.out.println(string1 == string2);
System.out.println(string1.equals(string2));

The first string contains the text “Hello”. We create another copy of this string.

Since the equality operator tests if both the variables contain the same reference, the first print statement prints false. In other words, the variables string1 and string2 hold two different references. However, they hold logically equal objects because they both contain the same text.

To prove that they contain the same text, we use the equals() method. This is why the second print statement prints true.

There are many other constructors implemented in the String class. We encourage you to learn more about these constructors from the Java documentation.