String Class Constructor in Java


The string class supports several types of constructors in Java APIs. The most commonly used constructors of String class is as follows.
1. String(): To create an empty String, we can call a default constructor.
For example:
      String s=new String(); 
It will create a string object in the heap area with no value. 

2. String(String str): It will create a string object in the heap area and stores the given value in it.
For example:
     String s2=new String("Hello Java");
Now, the object str contains Hello Java.

3. String(char chars[ ]): It will create a string object and stores the array of characters in it. 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 see an example program to understand the concept.
Program source code 1:
    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: science
4. String(char chars[ ], int startIndex, int count): It will create 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.
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.
Program source code 2: 
    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: wind
Program source code 3: 
In this example program, we will construct a String object that contains the same characters sequence as another string object.
    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 FAN
As you can see the output, s1 and s2 contain the same string. Thus, we can create one string from another string.
String constructor
5. String(byte byteArr[ ]): It constructor 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 see an example program to understand the concept.
Program source code 4:
    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: abcd
Key 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 source code 5:
    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: CDEF
These are several important constructors in String class, which is used for creating of string objects in different ways.
Final words 

Hope that this tutorial has covered all the important types of string constructor in Java with example programs. I hope that you will have enjoyed this tutorial.
Thanks for reading!