by Dinesh Thakur Category: array

The String object can be created explicitly by using the new keyword and a constructor in the same way as you have created objects in previously. For example The statement
String str = new String("Welcome to Java");

Creates a String object initialised to the value of the string literal "Welcome to Java" and assigns a reference to string reference variable str. Here, String ("Welcome to Java") is actually a constructor of the form String (string literals). When this statement compiled, the Java compiler invokes this constructor and initialises the String object with the string literal enclosed in parentheses which passed as an argument.
You can also create a string from an array of characters. To create a string initialised by an array of characters, use the constructor of the form
String (charArray)
For example, consider the following character array.
char[] charArray ={'H','i',' ','D','I','N','E','S','H'};
If we want to create a String object, str1 initialised to value contained in the character array chrArr, then use the following statement,
String str1 = new String(chrArr);
One should remember that the contents of the character array are copied, subsequent modification of the character array does not affect the newly created string.
In addition to these two constructors, String class also supports the following constructors,
String (): It constructs a new String object which is initialized to an empty string (" "). For example:
String s = new String();
Will create a string reference variable s that will reference an empty string.
String (String strObj): It constructs a String object which is initialized to same character sequence as that of the string referenced by strObj. For example, A string reference variable s references a String object which contains a string Welcome On execution of the following statement
String s2 = new String(s);
The string reference variable s2 will refer to a new String object with the same character sequence Welcome in it.

string reference variable
String (char [] chArr, int startIndex, int count): It constructs a new String object whose contents are the same as the character array, chArr, starting from index startlndex upto a maximum of count characters. For example: Consider the following statements
char[] str = {'W','e','l','c','o','m','e'};
String s3 = new String(str,5,2);
On execution, the string reference variable s3 will refer to a string object containing a character sequence me in it.
String (byte [] bytes): It constructs a new String object by converting the bytes array into characters using the default charset. For example, consider the following statements,
byte[] ascii ={65,66,67,68,70,71,73};
String s4 = new string (ASCII);
On execution, the string reference variable s4 will refer to a String object containing the character equivalent to the one in the bytes array.
String (byte [] bytes, int startIndex, int count): It constructs a new String object by converting the bytes array starting from index startIndex upto a maximum of count bytes into characters using the default charset. For example: On executing the statement,
String s5 = new String(ascii,2,3);
The string reference variable s5 will refer to a String object containing character sequence CDE.
String (byte [] bytes, String charsetName): It constructs a new String object by converting bytes array into characters using the specified charset.
String(byte[] bytes, int startIndex, int count, String charsetName): It constructs a new String object by converting the bytes array starting from index startIndex upto a maximum of count bytes into characters using the specified charset.
String (StringBuffer buffer): It constructs a new String object from a StringBuffer object.
Now let us consider an example to explain how these constructors are used.

public class StringConstructors

{
       public static void main(String[] args)
    {
          char[] charArray ={'H','i',' ','D','I','N','E','S','H'};
          byte[] ascii ={65,66,67,68,70,71,73};
          String str  = "Welcome";
          String strl =new String("Java");
          String str2 =new String(charArray);
          String str3 =new String(charArray,3,3);
          String str4 =new String(ascii);
          String str5 =new String(ascii,2,3);
          String str6 =new String();
          String str7 =new String(str);
          System.out.println("str : "+ str);
          System.out.println("strl : "+ strl);
          System.out.println("str2 : "+ str2);
          System.out.println("str3 : "+ str3);
          System.out.println("str4 : "+ str4);
          System.out.println("str5 : "+ str5);
          System.out.println("str6 : "+ str6);
          System.out.println("str7 : "+ str7);
          str += " Dinesh Thakur";
          System.out.println("str : "+ str);
    }
}

String Constructors in Java



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles