Package java.lang
Class String
- java.lang.Object
-
- java.lang.BString
-
- java.lang.String
-
- All Implemented Interfaces:
Serializable,Comparable<String>,Modified
public class String extends BString implements Serializable, Comparable<String>, Modified
An immutable sequence of characters/code units (chars). AStringis represented by array of UTF-16 values. This class is highly optimized version compared to the standard String class.Backing Arrays
This class is implemented using a char[]. The length of the array may exceed the length of the string. For example, the string "Hello" may be backed by the array['H', 'e', 'l', 'l', 'o', 'W'. 'o', 'r', 'l', 'd']with offset 0 and length 5.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description String(char[] value)Allocates a newStringso that it represents the sequence of characters currently contained in the character array argument.String(char[] value, int offset, int count)Allocates a newStringthat contains characters from a subarray of the character array argument.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static StringallocateString(int ref, char[] value)static StringallocateString(int ref, char[] value, int offset, int cnt)charcharAt(int index)Returns thecharvalue at the specified index.intcompareTo(String string)Compares the specified string to this string using the Unicode values of the characters.booleanequals(Object anObject)Compares the specified object to this string and returns true if they are equal.byte[]getBytes()Encodes thisStringinto a sequence of bytes using the platform's default charset, storing the result into a new byte array.voidgetChars(char[] dst, int dstBegin)Copy characters from this string into dst starting at dstBegin.intlength()Returns the length of this string.
-
-
-
Constructor Detail
-
String
public String(char[] value)
Allocates a newStringso that it represents the sequence of characters currently contained in the character array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string.- Parameters:
value- the initial value of the string.
-
String
public String(char[] value, int offset, int count)Allocates a newStringthat contains characters from a subarray of the character array argument. Theoffsetargument is the index of the first character of the subarray and thecountargument specifies the length of the subarray. The contents of the subarray are copied; subsequent modification of the character array does not affect the newly created string.- Parameters:
value- array that is the source of characters.offset- the initial offset.count- the length (count > 0).
-
-
Method Detail
-
allocateString
public static String allocateString(int ref, char[] value)
-
allocateString
public static String allocateString(int ref, char[] value, int offset, int cnt)
-
getBytes
public byte[] getBytes()
Encodes thisStringinto a sequence of bytes using the platform's default charset, storing the result into a new byte array.- Returns:
- The resultant byte array
-
getChars
public void getChars(char[] dst, int dstBegin)Copy characters from this string into dst starting at dstBegin. This method doesn't perform any range checking.- Parameters:
dst- The destination array.dstBegin- The start offset in the destination array.
-
charAt
public char charAt(int index)
Returns thecharvalue at the specified index. An index ranges from0tolength() - 1. The firstcharvalue of the sequence is at index0, the next at index1, and so on, as for array indexing.- Parameters:
index- the index of thecharvalue.- Returns:
- the
charvalue at the specified index of this string. The firstcharvalue is at index0.
-
equals
public boolean equals(Object anObject)
Compares the specified object to this string and returns true if they are equal. The object must be an instance of string with the same characters in the same order.- Overrides:
equalsin classObject- Parameters:
anObject- the object to compare.- Returns:
trueif the specified object is equal to this string,falseotherwise.- See Also:
Object.hashCode()
-
length
public int length()
Returns the length of this string. The length is equal to the number of 16-bit Unicode characters in the string.- Returns:
- number of characters in the string
-
compareTo
public int compareTo(String string)
Compares the specified string to this string using the Unicode values of the characters. Returns 0 if the strings contain the same characters in the same order. Returns a negative integer if the first non-equal character in this string has a Unicode value which is less than the Unicode value of the character at the same position in the specified string, or if this string is a prefix of the specified string. Returns a positive integer if the first non-equal character in this string has a Unicode value which is greater than the Unicode value of the character at the same position in the specified string, or if the specified string is a prefix of this string.- Specified by:
compareToin interfaceComparable<String>- Parameters:
string- the string to compare.- Returns:
- 0 if the strings are equal, a negative integer if this string is before the specified string, or a positive integer if this string is after the specified string.
- Throws:
NullPointerException- ifstringisnull.
-
-