- Java String Constructors
- Why do we need String Constructors?
- String Constructor Categories
- List of Java String Constructors
- String Constructor Examples
- 1. Creating Empty String
- 2. Creating String from Another String
- 3. Java String Constructor with Byte Array Argument
- 4. Creating String from Byte Array with Encoding
- 5. Creating String from Byte Array with Index
- 6. String Constructor with Character Array Argument
- 7. String Constructor with Code Points Array
- 8. String Constructor with StringBuffer Argument
- 9. String Constructor with StringBuilder Argument
- Conclusion
- String Constructors Java
Java String Constructors
Java String Constructors allows us to create a string object from different type of arguments.
Why do we need String Constructors?
We can create string object using double quotes.
So why do we need so many constructors in String Class?
A string is a sequence of characters. Sometimes we want to create a string object from different sources. For example, byte array, character array, StringBuffer, and StringBuilder. The String class constructors are provided to create a string object from these arguments.
String Constructor Categories
We can broadly classify string constructors into following categories.
- Creating Empty String
- Creating String from another string
- String from Byte Array
- String from Character Array
- String from Code Points
- String from StringBuffer and StringBuilder
List of Java String Constructors
- String() : creates an empty string. It’s mostly useless because String is immutable.
- String(String original) : creates a string object from another string. Since String is immutable, it’s of no use.
- String(byte[] bytes) : constructs a new string from the byte array using system default encoding.
- String(byte bytes[], String charsetName) : uses the specified character encoding. If the encoding is not supported, UnsupportedEncodingException is thrown.
- String(byte bytes[], Charset charset) : a better way to specify an encoding for constructing the string object.
- String(byte bytes[], int offset, int length) : The offset specifies the index of the first byte to decode. The length specifies the number of bytes to decode. This constructor throws IndexOutOfBoundsException if offset is negative, length is negative, or offset is greater than bytes.length — length .
- String(byte bytes[], int offset, int length, Charset charset) : It’s similar to the above constructor except that we have to specify the encoding to use.
- String(byte bytes[], int offset, int length, String charsetName) : Similar to above except that the character set encoding name is passed as a string. This will throw UnsupportedEncodingException if the encoding is not supported.
- String(char value[]) : creates the string object from the character array.
- String(char value[], int offset, int count) : The offset specifies the index of the first character. The length specifies the number of characters to use. This constructor throws IndexOutOfBoundsException if offset is negative, length is negative, or offset is greater than value.length — length .
- String(int[] codePoints, int offset, int count) : creates a string from the input Unicode code points array. It throws IllegalArgumentException if any of the code points are invalid. This constructor throws IndexOutOfBoundsException if the offset is negative, the length is negative, or offset is greater than codePoints.length — length .
- String(StringBuffer buffer) : creates a new string from the contents of the string buffer. This constructor internally calls StringBuffer toString() method.
- String(StringBuilder buffer) : creates a new string from the contents of the string builder.
String Constructor Examples
Let’s look at some code snippets to use the string constructors.
1. Creating Empty String
String s2 = new String(); // useless though s2 = ""; // better approach
2. Creating String from Another String
String s5 = new String("2019"); // useless since string is immutable
3. Java String Constructor with Byte Array Argument
byte[] bytes = "Java".getBytes(); System.out.println(Arrays.toString(bytes)); String s3 = new String(bytes); System.out.println(s3);
4. Creating String from Byte Array with Encoding
byte[] bytes = "Java".getBytes(); String s3 = null; try < s3 = new String(bytes, "UTF-8"); >catch (UnsupportedEncodingException e1) < e1.printStackTrace(); >System.out.println(s3); s3 = new String(bytes, StandardCharsets.UTF_8); System.out.println(s3);
5. Creating String from Byte Array with Index
byte[] bytes = "Java".getBytes(); String s3 = new String(bytes, 1, 2); System.out.println(s3); s3 = new String(bytes, 1, 3, StandardCharsets.US_ASCII); System.out.println(s3); try < s3 = new String(bytes, 1, 3, "UTF-8"); >catch (UnsupportedEncodingException e) < e.printStackTrace(); >System.out.println(s3);
6. String Constructor with Character Array Argument
char[] chars = "String Constructors".toCharArray(); System.out.println(Arrays.toString(chars)); String s4 = new String(chars); System.out.println(s4); s4 = new String(chars, 2, 8); System.out.println(s4);
[S, t, r, i, n, g, , C, o, n, s, t, r, u, c, t, o, r, s] String Constructors ring Con
7. String Constructor with Code Points Array
int[] codePoints = < 74, 97, 118, 97 >; String s6 = new String(codePoints, 0, 2); System.out.println(s6); s6 = new String(codePoints, 0, codePoints.length); System.out.println(s6);
8. String Constructor with StringBuffer Argument
StringBuffer sb = new StringBuffer("Hello"); sb.append("World"); String s7 = new String(sb); System.out.println(s7); s7 = sb.toString(); // better approach System.out.println(s7);
9. String Constructor with StringBuilder Argument
StringBuilder sb1 = new StringBuilder("Java "); sb1.append("String").append(" Class"); String s8 = new String(sb1); System.out.println(s8); s8 = sb1.toString(); // better approach System.out.println(s8);
Conclusion
Java String provides a lot of constructors for general purpose requirements. If you are creating a new string, try to use a string literal. You can use the constructors to create the string from a byte array, char array, and code points. Always use StringBuffer and StringBuilder toString() method to create their string representation.
String Constructors Java
1. public java.lang.String();
2. public java.lang.String(java.lang.String);
3. public java.lang.String(char[]);
4. public java.lang.String(char[], int startIndex, int numChars);
5. public java.lang.String(int[], int, int);
6. public java.lang.String(byte[]);
7. public java.lang.String(byte[], int startIndex, int numInts);
8. public java.lang.String(java.lang.StringBuffer);
9. public java.lang.String(java.lang.StringBuilder);
The following is the example code using the above 9 string constructors
public class Demo < public static void main(String args[]) < String str1 = new String(); // using 1st default constructor System.out.println("1st constructor: " + str1); // does not print any value String str2 = new String("Second constructor"); // using 2nd constructor System.out.println("2nd constructor: " + str2); // prints Second constructor char charArray1[] = ; String str3 = new String(charArray1); // using 3rd constructor System.out.println("3rd constructor: " + str3); // prints abcdefgh String str4 = new String(charArray1, 1, 6); // using 4th constructor System.out.println("4th constructor: " + str4); // prints bcdefg, from 1 onwards 6 characters int intArray1[]=; // ASCII numbers are converted to characters implicitly String str5 = new String(intArray1, 1, 6); // using 5th constructor System.out.println("5th constructor: " + str5); // prints BCDEFG byte bytetArray1[] = ; // ASCII numbers are converted to characters implicitly String str6 = new String(bytetArray1); // using 6th constructor System.out.println("6th constructor: " + str6); // prints ABCDEFGH String str7 = new String(bytetArray1, 1, 6); // using 7th constructor System.out.println("7th constructor: " + str7); // prints BCDEFG StringBuffer sb1 = new StringBuffer("Jyostna"); String str8 = new String(sb1); // using 8th constructor System.out.println("8th constructor: " + str8); // prints Jyostna StringBuilder sb2 = new StringBuilder("Jyothi"); String str9 = new String(sb2); // using 9th constructor System.out.println("9th constructor: " + str9); // prints Jyothi > >
Output screen on String Constructors Java
Also you may be interested in Java StringBuffer Constructors