String constructors in java

In this post we are going to learn string constructors in java.

String constructors in java

Creating string constructor is same as creating object. String constructor is created using “new” keyword. For example,

// String("Flower Brackets") is constructor
String str = new String("Flower Brackets");

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

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

Below are list of class String constructor with example,

String(char charArr[]): this allocates a new string from java character array.

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

String(byte[] byte_arr): this constructs a new String by decoding the specified array of bytes using the platform’s default charset.

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

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[] byteArr, int startIndex, int length): this constructs new string by decoding of the byte array depending on startIndex(Starting location) and length(number of characters from starting location), it used charset for decoding.

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.

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

String(char charArray[], int startIndex, int count): allocates a new string to given character array it chooses count characters from the start index.

char charArr[] = {'h', 'e', 'l', 'l', 'o'};
String str = new String(charArr, 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.

String buffer = "hello";
String str = new String(buffer);
System.out.println(str); // Hello

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

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

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 – learn string and its methods

Reference – oracle docs.