String constructors in java

Let’s learn string constructors in java.

String class constructors in java

String constructors in java

A string is an object of class class String. String constructors are used for creating and initializing string objects.

Java string constructor is created using “new” keyword. Below are list of constructors of string class in java,

String str = new String(); it creates empty string object. For example,

String str = new String(); // empty object is created
// here reference variable "str" length will be zero


String str = new String(“string literal”); creates string object on heap for given string literal.

class Demo
{
   public static void main(String args[])
   {
      // String("Flower Brackets") is constructor
      String str = new String("FlowerBrackets"); // creates object in heap area
      System.out.println(str);  // FlowerBrackets
   }
}


Here first String object is created and initialized to value “FlowerBrackets” and assigned to reference variable “str”.

Java compiler invokes String(“FlowerBrackets”) constructor and initializes String object in between parentheses which is passed as argument.

String str = new String(char ch[]): this creates a new string from java character array.

char ch[] = {'h', 'e', 'l', 'l', 'o'};
String str = new String(ch);
System.out.println(str);  // hello


String str = new String(byte arr[]): this creates and initializes a new String from byte array. These byte array elements ASCII value is converted to string.

byte arr[] = {88, 89, 90};
String str = new String(arr);
System.out.println(str); // XYZ


String(byte[] byte_arr, Charset char_set): this constructs a new String by decoding the specified array of bytes using the specified charset.

byte bArr[] = {99, 114, 108, 102, 102};
Charset ch = Charset.defaultCharset();
String str = new String(bArr, ch);
System.out.println(str); // crlff


String(byte[] byte_arr, String char_set_name): this constructs new string by decoding of the byte array using the charsetname of the platform for decoding.

byte bArr[] = {99, 114, 108, 102, 102};
String str = new String(bArr, "US-ASCII");
System.out.println(str); // crlff


String(byte arr[], int start, int length):

this constructs new string by decoding of the byte array depending on start(Starting location) and length(number of characters from starting location), it used charset for decoding.

byte arr[] = {99, 114, 108, 102, 102};
String str = new String(arr, 1, 3);
System.out.println(str);  // rlf


String(byte[] byte_arr, int start_index, int length, Charset char_set): Constructs a new String by decoding the specified subarray of bytes using the specified charset.

The length of the new String is a function of the charset, and hence may not be equal to the length of the subarray.

byte bArr[] = {99, 114, 108, 102, 102};
Charset ch = Charset.defaultCharset();
String str = new String(bArr, 1, 3, ch);
System.out.println(str);  // rlf


String(byte[] byteArr, int startIndex, int length, String charsetName): this constructs new string by decoding of the byte array depending on the startIndex(Starting location) and length(number of characters from starting location), it uses charsetName for decoding.

byte bArr[] = {99, 114, 108, 102, 102};
String str = new String(bArr, 1, 4, "US-ASCII");
System.out.println(str);  // rlff


String(String original) : this initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.

String str = new String("HelloWorld");
System.out.println(str); // HelloWorld


In Strings, null values are not allowed. For more on this constructor click here.

String(char ch[], int start, int count): allocates a new string and initializes with sub-array “ch” from index “start”. “count” variable counts number of characters.

char ch[] = {'h', 'e', 'l', 'l', 'o'};
String str = new String(ch, 1, 3);
System.out.println(str);  // ell

String(StringBuffer buffer): this constructor allocates a new string that contains the sequence of characters currently contained in the string buffer argument.

StringBuffer sb = new StringBuffer("Java");
String str = new String(sb);
System.out.println(str); // Java


String(StringBuilder builder): this constructor allocates a new string that contains the sequence of characters currently contained in the string builder argument.

StringBuilder sb = new StringBuilder("Java");
String str = new String(sb);
System.out.println(str);  //Java


String(int[] codePoints, int offset, int count): this allocates a new String that contains characters from a subarray of the Unicode code point array argument.

int uniCode[] = {99, 114, 108, 102, 102};
String str = new String(uniCode, 1, 3);
System.out.println(str); // rlf


Also read – interface in java

Reference – oracle docs.