The string constructors
The String class supports various constructors. To make a null String, you call the default constructor. instance,
String s = new String ();
will create an object of string along with no characters in it.
Often, you will need to create strings which have initial values. A string class provides a variety of constructors to handle this. For create a string initialized through an array of characters, use the constructor displays here:
String(char c[ ]) (Char Chars [ ])
Here is an instance
Char chars[]={'a','b','c'};
String s = new String(c);
This constructor initializes s along with the string "abc".
You could specify a sub range of a character array as an initializer by using the subsequent constructor:
String(char chars[],int startIndex, int numChars)
In the given syntax, startIndex specifies the index at that the subrange starts, and numChars specifies the number of characters to use.
Here is an example:
char chars[]= { 'a', 'b','c','d','e','f',};
String s = new String(chars,2,3);
This initializes s along with the characters cde.
You can construct a string object which holds the similar character sequence as another string object by using this constructor:
String(String s)
Here, s is a string object. Let consider this example:
//Construct one string from another.
class MakeString {
public static void main(String a[ ]) {
char c[] = { 'J','a','v','a'};
String s1 = new String(c);
String s2 = new String(s1);
System.out.println(s1);
System.out.println(s2);
}
}
The output is as follows:
Java
Java
As you could see s1, s2 holds the similar string.