Tuesday, February 28, 2012

STRING CONSTRUCTOR IN JAVA

String Constructors
         The String class supports several constructors. To create an empty String, you call the default constructor.


Example:

String s = new String();

       It will create an instance of String with no characters in it.
Frequently, you will want to create strings that have initial values.
The String class provides a variety of constructors to handle this.

To create a String initialized by an array of characters

String(char chars[ ])
 Example:
char chars[] = { 'a', 'b', 'c' };
String s = new String(chars);
 This constructor initializes s with the string “abc”.
You can specify a subrange of a character array as an initializer using the following constructor:
 String(char chars[ ], int startIndex, int numChars)
Here, startIndex specifies the index at which the subrange begins, and numChars specifies the number of characters to use.

Example:
 
char chars[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
String sam = new String(chars, 2, 3);
This initializes s with the characters cde. You can construct a String object that contains the same character sequence as another String object using this constructor:
String(String str)
Here, str is a String object.

Example:
class CreateString {
public static void main(String args[]) {
char c[] = {'J', 'a', 'v', 'a'};
String s1 = new String(c);
String s2 = new String(s1);
System.out.println(s1);
System.out.println(s2);
}
}
output:
Java
Java

  s1 and s2 contain the same string.
VA RY
  • Even though Java’s char type uses 16 bits to represent the Unicode character set, the typical format for strings on the Internet uses arrays of 8-bit bytes constructed from the ASCII character set.
  • Because 8-bit ASCII strings are common, the String class provides constructors that initialize a string when given a byte array.

String(byte asciiChars[ ])
String(byte asciiChars[ ], int startIndex, int numChars)
  • asciiChars specifies the array of bytes.
  • The second form allows you to specify a subrange. In each of these constructors, the byte-to-character conversion is done by using the default character encoding of the platform. 

Example:
 class SubStringCons {
public static void main(String args[]) {
byte ascii[] = {65, 66, 67, 68, 69, 70 };
String s1 = new String(ascii);
System.out.println(s1);
String s2 = new String(ascii, 2, 3);
System.out.println(s2);
}
}
output:
ABCDEF
CDE
  • Extended versions of the byte-to-string constructors are also defined in which you can specify the character encoding that determines how bytes are converted to characters.
  • The contents of the array are copied whenever you create a String object from an array. If you modify the contents of the array after you have created the string, the String will be unchanged.
 String Length
        The length of a string is the number of characters that it contains. To obtain this value, call the length( ) method
int length( )
The following fragment prints “3”, since there are three characters in the string s: 
char chars[] = { 'a', 'b', 'c' };
String s = new String(chars);
System.out.println(s.length());

No comments:

Post a Comment