Powered by Blogger.

Sunday 29 May 2016

The String Constructors

No comments :
The String class supports several constructors. To create an empty String, call the default constructor. for example,

will create an instance of String with no characters in it.
The String class provides a variety of constructor to handle this. To create a String initialized by an array of characters, use the constructor show here:

Here is an example:

This constructor initializes s with the string "manoj".
You can secify a subrange of a character array as an initializer using the following constructor:

Here, startIndex specifies the index at which the subrange begins, namd numChars specifies the number of characters to use. Here is an example:

This initializes s which the characters "noj".
You can construct a String object that contains the same character sequence as another String object using this constructor:

Here, strObj is String object. consider this example:

This programming generates the following output:
manoj
manoj
As you can see, s1 and s2 contain the same String.
Java's char type uses 16 bit to represent the basic Unicode character set, the typical formate for strings on the Internet uses arrays of 8-bit bytes constructed from the ASCII characters that initialize a string when given a byte array. Two forms are show here:

Here is an example:

This programming generates the following output:
MANOJ
NOJ

You can construct a string from a StringBuffer by using the constructor shown here:

You can construct a string from a StringBuilder by using the constructor shown here:

The following constructor supports the extended Unicode character set:

Here, codePrints is an array that contains Unicode code points.

No comments :

Post a Comment

Please Write a Message for Programming or something Related issues.