String constructors in java

Hi!!! Welcome to flower brackets blog. In this post we are going to learn string constructors in java.

String constructors in java

String constructor can be created using “new” keyword. For example,

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

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

Below are list of class String constructor with example,

String(): constructs new empty string.

String str = new String(); // reference variable "str" will reference an empty string

String(String obj): constructs new String object. This String object will be initialized to character sequence as referenced by “obj”.

String(byte[] bytes): constructs new string by decoding given array of bytes using platform default charset.

byte arr[] = {74, 97, 118, 97};
String str = new String(arr);
System.out.println(str); // Java

String(byte[] bytes, Charset charset) : constructs new string by decoding given array of bytes using specified charset.

byte arr[] = {74, 97, 118, 97};
Charset ch = Charset.defaultCharset();
String str = new String(arr, ch);
System.out.println(str); // Java

String(byte[] bytes, int offset, int length) : constructs new string by decoding given subarray of bytes using platform’s default charset.

byte bytes[] = {76,77,78,79,80,81,82};
String str = new String(bytes,1,4);
System.out.println(str); // MNOP

String(byte[] bytes, int offset, int length, Charset charset) : constructs new string by decoding given subarray of bytes using specified charset.

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

String(byte[] bytes, int offset, int length, String charsetName) : constructs new string by decoding given subarray of bytes using specified charset.

byte arr[] = { 'h', 'e', 'l', 'l', 'o' };
try
{
String str = new String(arr, 1, 3, "UTF-8");
System.out.println(str); // ell
}
catch(UnsupportedEncodingException ex)
{
ex.printStackTrace();
}

String(byte[] bytes, String charsetName) : Constructs new string by decoding specified array of bytes using the specified charset.

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

String(char[] value) : here it allocates a new String so that it represents the sequence of characters currently contained in the character array argument.

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

String(char[] value, int offset, int count) : this allocates a new String that contains characters from a subarray of the character array argument.

char[] ch1 = new char[] { 'j', 'a', 'v', 'a' };
String s1 = String.valueOf(ch1, 2, 2);
char[] ch2 = new char[] { '8', '2', '3' };
String s2 = String.valueOf(ch2, 1, 2);
System.out.println(s1); // va
System.out.println(s2); // 23

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 arr[] = new int[26];
int num = 97;
for(int a = 0; a < 26; a++)
{
arr[a] = num + a;
}
String str = new String(arr, 0, 26);
System.out.println(str); // abcdefghijklmnopqrstuvwxyz

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(StringBuffer buffer) : this allocates a new string that contains the sequence of characters currently contained in the string buffer argument.

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

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

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

Also read – learn string and its methods

Reference – oracle docs.

Related Posts