Tuesday, March 27, 2012

The String Constructors

The String class supports several constructors. To create an empty String, you call the
default constructor. For example,
String s = new String();
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, use the constructor shown here:

String(char chars[ ])

Here is an 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. Here is an example:

char chars[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
String s = 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 strObj)

Here, strObj is a String object. Consider this example:

// Construct one String from another.
class MakeString {
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);
}
}


The output from this program is as follows:
Java
Java
As you can see, s1 and s2 contain the same string.

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. Their forms are shown here:
String(byte asciiChars[ ])
String(byte asciiChars[ ], int startIndex, int numChars)
Here, 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. The following program illustrates
these constructors:


// Construct string from subset of char array.
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);
}
}


This program generates the following 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. However, most of the time, you will want to use the default encoding
provided by the platform.

Note :
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, shown here:
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