Creating String object – String constructors

String class provides many constructors to create a new String object from character array, byte array, StringBuffer, StringBuilder, etc.

Constructor Purpose
String() Creates an empty string.
String(String) Creates a string from the specified string.
String(char[]) Creates a string from an array of characters.
String(char[], int offset, int count) Creates a string from the specified subset of characters in an array.
String (byte[] byte) Constructs a new String by decoding the specified array of bytes using the platform’s default charset.
String (byte[] byte, int offset, int len) Constructs a new String by decoding the specified subarray of bytes using the platform’s default charset.
String(byte[] bytes, int offset, int length, String charsetName) Constructs a new String by decoding the specified subarray of bytes using the specified charset.
String(byte[] bytes, String charsetName) Constructs a new String by decoding the specified array of bytes using the specified charset.
String(StringBuffer) Creates a string from StringBuffer argument.
String(StringBuilder) Creates a string from StringBuilder argument.

Creating an empty String

package com.ibytecode.strings.construction;
public class CreatingStringsUsingConstrutcor {
	public static void main(String[] args) {
		String s = new String();
		System.out.println("Length = " + s.length());
	}	
}

Length = 0

Creating a String object with value

You can create a new String object that contains the same character sequence as another String object.

String str = new String("I love Java");
System.out.println("str = " + str);

str = I love Java

char array to String

You can create a String object from character array. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string.

Constructors used
public String(char[] value)
public String(char[] value,int offset, int count) throws IndexOutOfBoundsException
where,
value – Array that is the source of characters
offset – The initial offset
count – The length
package com.ibytecode.strings.construction;
public class CreatingStringsUsingConstrutcor {
	public static void main(String[] args) {
		char chars[] = { 'i', 'B', 'y', 't', 'e', 'C', 'o', 'd', 'e'};
		String s2 = new String(chars);
		String s3 = new String(chars, 1, 4); 
		System.out.println("s2 = " + s2);
		System.out.println("s3 = " + s3);
	}
}

s2 = iByteCode
s3 = Byte

byte array to String

You can create a String object from byte array.

Constructors used
public String(byte[] bytes)
public String(byte[] bytes, int offset, int length)

where,
bytes – The bytes to be decoded into characters
offset – The index of the first byte to decode
length – The number of bytes to decode

public String(byte[] bytes, Charset charset)
public String(byte[] bytes, String charsetName) throws UnsupportedEncodingException
public String(byte[] bytes, int offset, int length, Charset charset) throws IndexOutOfBoundsException
public String(byte[] bytes, int offset, int length, String charsetName) throws UnsupportedEncodingException
byte byteAscii[] = {65, 66, 67, 68, 69, 70 };
String strAscii = new String(byteAscii);
System.out.println("strAscii = " + strAscii);

strAscii = ABCDEF

byte bytes[] = { 'w', 'o', 'r', 'l', 'd' };
String s4 = new String(bytes);
System.out.println("s4 = " + s4);

s4 = world

byte bytes[] = { 'w', 'o', 'r', 'l', 'd' };
String s5 = new String(bytes, 1, 3);
System.out.println("s5 = " + s5);

s5 = orl

byte bytes[] = { 'w', 'o', 'r', 'l', 'd' };
try {
	String s7 = new String(bytes, 1, 3, "UTF-8");
	System.out.println("s7 = " + s7);
     } catch (UnsupportedEncodingException e) {
	e.printStackTrace();
}

s7 = orl

Constructing String from StringBuffer

You can create a new String from a string buffer. The contents of the string buffer are copied; subsequent modification of the string buffer does not affect the newly created string.

StringBuffer sb = new StringBuffer("theopentutorials.com");
String s8 = new String(sb);
System.out.println("s8 = " + s8);

s8 = theopentutorials.com

Constructing String from StringBuilder

You can create a new String from a string builder. The contents of the string builder are copied; subsequent modification of the string builder does not affect the newly created string.

StringBuilder builder = new StringBuilder("ibytecode.com");
String s9 = new String(builder);
System.out.println("s9 = " + s9);

s9 = ibytecode.com

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.