In Java, a String object can be created a number of ways. The most frequently used styles are shown here using 9 types of String constructors (including StringBuffer and StringBuilder) defined java.lang.String API.
The 9 constructors are
1. public java.lang.String();
2. public java.lang.String(java.lang.String);
3. public java.lang.String(char[]);
4. public java.lang.String(char[], int startIndex, int numChars);
5. public java.lang.String(int[], int, int);
6. public java.lang.String(byte[]);
7. public java.lang.String(byte[], int startIndex, int numInts);
8. public java.lang.String(java.lang.StringBuffer);
9. public java.lang.String(java.lang.StringBuilder);
The following is the example code using the above 9 string constructors
public class Demo
{
public static void main(String args[])
{
String str1 = new String(); // using 1st default constructor
System.out.println("1st constructor: " + str1); // does not print any value
String str2 = new String("Second constructor"); // using 2nd constructor
System.out.println("2nd constructor: " + str2); // prints Second constructor
char charArray1[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
String str3 = new String(charArray1); // using 3rd constructor
System.out.println("3rd constructor: " + str3); // prints abcdefgh
String str4 = new String(charArray1, 1, 6); // using 4th constructor
System.out.println("4th constructor: " + str4); // prints bcdefg, from 1 onwards 6 characters
int intArray1[]={65, 66, 67, 68, 69, 70, 71, 72}; // ASCII numbers are converted to characters implicitly
String str5 = new String(intArray1, 1, 6); // using 5th constructor
System.out.println("5th constructor: " + str5); // prints BCDEFG
byte bytetArray1[] = {65, 66, 67, 68, 69, 70, 71, 72}; // ASCII numbers are converted to characters implicitly
String str6 = new String(bytetArray1); // using 6th constructor
System.out.println("6th constructor: " + str6); // prints ABCDEFGH
String str7 = new String(bytetArray1, 1, 6); // using 7th constructor
System.out.println("7th constructor: " + str7); // prints BCDEFG
StringBuffer sb1 = new StringBuffer("Jyostna");
String str8 = new String(sb1); // using 8th constructor
System.out.println("8th constructor: " + str8); // prints Jyostna
StringBuilder sb2 = new StringBuilder("Jyothi");
String str9 = new String(sb2); // using 9th constructor
System.out.println("9th constructor: " + str9); // prints Jyothi
}
}
Output screen on String Constructors Java
Also you may be interested in Java StringBuffer Constructors