The String Constructors
The String class supports several constructors. To create an empty String, you call the default constructor, For example, This constructor initializes s with the string” abc”, You can specify a sub range of a character array as an initialize using the following constructor: Stringiness chars[ ], int start index, int Mere, starring specifies the index at which the sub range begins, and (/rs specifics the number of characters to use. Here is an example This initializes s with the characters code. You can construct a String object that contains the same character sequence as another String’object using this constructor: . String(String strobe)
Here, strobe is a String object. Consider this example: 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 stirrings are common, the String class provides· constructors that initialize string where; given a byte array. Their forms are shown here: String type Richards String, (byte scimitar , int Strickland, int chancellors) Here, Richards specifics the array of bytes. The second form allows you to specify a sub range. 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: 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.
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: tint length( ) The following fragment prints “3”,since there are three characters in the string s: