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 (char
s). AString
is 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 newString
so that it represents the sequence of characters currently contained in the character array argument.String(char[] value, int offset, int count)
Allocates a newString
that 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 String
allocateString(int ref, char[] value)
static String
allocateString(int ref, char[] value, int offset, int cnt)
char
charAt(int index)
Returns thechar
value at the specified index.int
compareTo(String string)
Compares the specified string to this string using the Unicode values of the characters.boolean
equals(Object anObject)
Compares the specified object to this string and returns true if they are equal.byte[]
getBytes()
Encodes thisString
into a sequence of bytes using the platform's default charset, storing the result into a new byte array.void
getChars(char[] dst, int dstBegin)
Copy characters from this string into dst starting at dstBegin.int
length()
Returns the length of this string.
-
-
-
Constructor Detail
-
String
public String(char[] value)
Allocates a newString
so 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 newString
that contains characters from a subarray of the character array argument. Theoffset
argument is the index of the first character of the subarray and thecount
argument 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 thisString
into 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 thechar
value at the specified index. An index ranges from0
tolength() - 1
. The firstchar
value of the sequence is at index0
, the next at index1
, and so on, as for array indexing.- Parameters:
index
- the index of thechar
value.- Returns:
- the
char
value at the specified index of this string. The firstchar
value 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:
equals
in classObject
- Parameters:
anObject
- the object to compare.- Returns:
true
if the specified object is equal to this string,false
otherwise.- 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:
compareTo
in 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
- ifstring
isnull
.
-
-