String Constructor in Java

String objects can be created through constructors. The following constructors are supported:
public String()
Create a new String with the value "" containing no characters. The value is not null.
public String( String  str )
Create a new String that contains the same sequence of characters as the specified String argument.
public String( char[] strData )
Create a new String containing the same sequence of characters contained in the character array argument.
String str1 = new String();
String str2 = new String( "Java" ); // "Java"  first converted to a String and then
// passed as an String argument
String str3 = new String( str2 );
char charArray[] = { 'J', 'a', 'v', 'a' };

String str4 = new String( charArray );

SN

Constructor 

1

String()
          Initializes a newly created String object so that it represents an empty character sequence.

2

String(byte[] bytes)
          Constructs a new String by decoding the specified array of bytes using the platform's default charset.

3

String(byte[] ascii, int hibyte)
          Deprecated. This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the String constructors that take a charset name or that use the platform's default charset.

4

String(byte[] bytes, int offset, int length)
          Constructs a new String by decoding the specified subarray of bytes using the platform's default charset.

5

String(byte[] ascii, int hibyte, int offset, int count)
          Deprecated. This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the String constructors that take a charset name or that use the platform's default charset.

6

String(byte[] bytes, int offset, int length, String charsetName)
          Constructs a new String by decoding the specified subarray of bytes using the specified charset.

7

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

8

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

9

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

10

String(String original)
          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.

11

String(StringBuffer buffer)
          Allocates a new string that contains the sequence of characters currently contained in the string buffer argument.

 



 

Latest Tutorial


Most Visited Tutorial