In this tutorial, we will understand the most commonly used string constructor in Java with the help of example programs.
In Java, string class supports several types of constructors to create and initialize string objects based on the various types of arguments. This is an example of polymorphism.
The most commonly used constructors of the String class are as follows:
- String()
- String(String str)
- String(char chars[ ])
- String(char chars[ ], int startIndex, int count)
- String(byte byteArr[ ])
- String(byte byteArr[ ], int startIndex, int count)
Let’s understand all the above constructors provided by Java string class one by one.
1. String(): To create an empty string, we will call the default constructor. The general syntax to create an empty string in Java program is as follows:
String s = new String();It will create a string object in the heap area with no value.
[blocksy-content-block id=”12371″]
2. String(String str): This is the most common string constructor that we generally use in the Java program. This constructor will create a new string object in the heap area and stores the given value in it. The general syntax to construct a string object with the specified string str is as follows:
String st = new String(String str);
For example:
String s2 = new String("Hello Java");Here, the object str contains Hello Java.
3. String(char chars[ ]): This string constructor creates a string object and stores the array of characters in it. The general syntax to create a string object with a specified array of characters is as follows:
String str = new String(char char[])
For example:
char chars[] = { 'a', 'b', 'c', 'd' };
String s3 = new String(chars);
The object reference variable s3 contains the address of the value stored in the heap area. Let’s take an example program where we will create a string object and store an array of characters in it.
[blocksy-content-block id=”12121″]
Program code:
package stringPrograms;
public class Science {
public static void main(String[ ] args)
{
char chars[] = {'s', 'c', 'i', 'e', 'n', 'c', 'e'};
String s = new String(chars);
System.out.println(s);
}
}Output:
science4. String(char chars[ ], int startIndex, int count): This constructor creates and initializes a string object with a subrange of a character array.
The argument startIndex specifies the index at which the subrange begins and count specifies the number of characters to be copied. The general syntax to create a string object with the specified subrange of character array is as follows:
String str = new String(char chars[ ], int startIndex, int count);
For example:
char chars[] = { 'w', 'i', 'n', 'd', 'o', 'w', 's' };
String str = new String(chars, 2, 3);The object str contains the address of the value ”ndo” stored in the heap area because the starting index is 2 and the total number of characters to be copied is 3. Let’s take an example program based on it.
Program code:
package stringPrograms;
public class Windows {
public static void main(String[] args)
{
char chars[] = { 'w', 'i', 'n', 'd', 'o', 'w', 's' };
String s = new String(chars, 0,4);
System.out.println(s);
}
}Output:
windLet’s create a Java program where we will create a string object that contains the same characters sequence as another string object.
[blocksy-content-block id=”12153″]
Program code:
package stringPrograms;
public class MakeString {
public static void main(String[] args)
{
char chars[] = { 'F', 'A', 'N' };
String s1 = new String(chars);
String s2 = new String(s1);
System.out.println(s1);
System.out.println(s2);
}
}Output:
FAN
FANAs you can see in the output, s1 and s2 contain the same string. Thus, we can create one string from another string.
5. String(byte byteArr[ ]): This type of string constructor constructs a new string object by decoding the given array of bytes (i.e., by decoding ASCII values into the characters) according to the system’s default character set. Let’s take an example program based on this constructor.
Program code:
package stringPrograms;
public class ByteArray {
public static void main(String[] args)
{
byte b[] = { 97, 98, 99, 100 }; // Range of bytes: -128 to 127. These byte values will be converted into corresponding characters.
String s = new String(b);
System.out.println(s);
}
}Output:
abcdKey points:
1. 97 is the Unicode value of a, 98 ➨ b, 99 ➨ c, 100 ➨ d.
2. 65 is the Unicode value of A.
6. String(byte byteArr[ ], int startIndex, int count): This constructor also creates a new string object by decoding the ASCII values using the system’s default character set.
Program code:
package stringPrograms;
public class ByteArray {
public static void main(String[] args)
{
byte b[] = { 65, 66, 67, 68, 69, 70 }; // Range of bytes: -128 to 127.
String s = new String(b, 2, 4); // CDEF
System.out.println(s);
}
}Output:
CDEFThese are several important constructors in the String class that are used for creating string objects in different ways. By understanding and applying the appropriate string constructor for your needs, you can write more efficient, readable, and understandable Java program code.
I hope that you will have understood all the important string constructors provided by Java string class and practiced all example programs based on them.





