STRING CONSTRUCTORS

The String class supports several constructors.

Class constructors

S.N.Constructor & Description
1
String()
This initializes a newly created String object so that it represents an empty character sequence.
String s = new String();
2
String(byte[] bytes)
This constructs a new String by decoding the specified array of bytes using the platform's default charset.
byte ascii[] = {65, 66, 67, 68, 69, 70 }; 
String s1 = new String(ascii); 
3
String(byte[] bytes, Charset charset)
This constructs a new String by decoding the specified array of bytes using the specified charset.
  byte[] bytes = new byte[]{65,66,67,68};
  System.out.println(new String(bytes, Charset.forName("ASCII")));
4
String(byte[] bytes, int offset, int length)
This constructs a new String by decoding the specified subarray of bytes using the platform's default charset
 byte ascii[] = { 65, 66, 67, 68, 69, 70 };
 String s2 = new String(ascii, 2, 3);
5
String(byte[] bytes, int offset, int length, Charset charset)
This constructs a new String by decoding the specified subarray of bytes using the specified charset.
 byte[] bytes = new byte[]{65,66,67,68};
 System.out.println(new String(bytes,1,3, Charset.forName("ASCII")));
6
String(byte[] bytes, int offset, int length, String charsetName)
This constructs a new String by decoding the specified subarray of bytes using the specified charset.
byte[] bytes = new byte[]{65,66,67,68};
System.out.println(new String(bytes,1,3,"ASCII"));
7
String(byte[] bytes, String charsetName)
This constructs a new String by decoding the specified array of bytes using the specified charset.
8
String(char[] value)
This allocates a new String so that it represents the sequence of characters currently contained in the character array argument.
char c[] = {'J', 'a', 'v', 'a'}; 
String s1 = new String(c);
9
String(char[] value, int offset, int count)
This allocates a new String that contains characters from a subarray of the character array argument.
10
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.
11
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.
12
String(StringBuffer buffer)
This allocates a new string that contains the sequence of characters currently contained in the string buffer argument.
13
String(StringBuilder builder)
This allocates a new string that contains the sequence of characters currently contained in the string builder argument.

No comments:

Post a Comment