String Constructor in Java with Example

Java String Constructor | String class supports several types of constructors in Java APIs.

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.

2. String(String str): It will create a string object in the heap area and stores the given value in it. The general syntax to construct a string object with 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 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.


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: 
           science

4. 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: 
       wind

Let’s create a Java program where we will create a String object that contains the same characters sequence as another string object.

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 
         FAN

As 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 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: 
       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 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: 
            CDEF

These are several important constructors in the String class that are used for creating string objects in different ways.


Hope that this tutorial has covered all the important types of string constructor in Java with example programs. I hope that you will have understood java string constructor and enjoyed this tutorial.
Thanks for reading!!!
Next ⇒ StringBuffer Class in Java⇐ PrevNext ⇒

Please share your love