Package java.lang

Class 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). A String 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
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected char[] value
      value is used for character storage.
    • Constructor Summary

      Constructors 
      Constructor Description
      String​(char[] value)
      Allocates a new String so that it represents the sequence of characters currently contained in the character array argument.
      String​(char[] value, int offset, int count)
      Allocates a new String 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 the char 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 this String 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.
    • Field Detail

      • value

        protected char[] value
        value is used for character storage.
    • Constructor Detail

      • String

        public String​(char[] value)
        Allocates a new String 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 new String that contains characters from a subarray of the character array argument. The offset argument is the index of the first character of the subarray and the count 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 this String 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 the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.
        Parameters:
        index - the index of the char value.
        Returns:
        the char value at the specified index of this string. The first char value is at index 0.
      • 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 class Object
        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 interface Comparable<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 - if string is null.