< prev index next >

src/java.base/share/classes/java/lang/String.java

Print this page

  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.ObjectStreamField;
  29 import java.io.UnsupportedEncodingException;
  30 import java.lang.annotation.Native;


  31 import java.lang.invoke.MethodHandles;
  32 import java.lang.constant.Constable;
  33 import java.lang.constant.ConstantDesc;
  34 import java.nio.ByteBuffer;
  35 import java.nio.CharBuffer;
  36 import java.nio.charset.*;
  37 import java.util.ArrayList;
  38 import java.util.Arrays;
  39 import java.util.Comparator;
  40 import java.util.Formatter;
  41 import java.util.List;
  42 import java.util.Locale;
  43 import java.util.Objects;
  44 import java.util.Optional;
  45 import java.util.Spliterator;
  46 import java.util.function.Function;
  47 import java.util.regex.Pattern;
  48 import java.util.regex.PatternSyntaxException;
  49 import java.util.stream.Collectors;
  50 import java.util.stream.IntStream;

1819      }
1820 
1821     /**
1822      * Encodes this {@code String} into a sequence of bytes using the
1823      * {@link Charset#defaultCharset() default charset}, storing the result
1824      * into a new byte array.
1825      *
1826      * <p> The behavior of this method when this string cannot be encoded in
1827      * the default charset is unspecified.  The {@link
1828      * java.nio.charset.CharsetEncoder} class should be used when more control
1829      * over the encoding process is required.
1830      *
1831      * @return  The resultant byte array
1832      *
1833      * @since      1.1
1834      */
1835     public byte[] getBytes() {
1836         return encode(Charset.defaultCharset(), coder(), value);
1837     }
1838 















1839     /**
1840      * Compares this string to the specified object.  The result is {@code
1841      * true} if and only if the argument is not {@code null} and is a {@code
1842      * String} object that represents the same sequence of characters as this
1843      * object.
1844      *
1845      * <p>For finer-grained String comparison, refer to
1846      * {@link java.text.Collator}.
1847      *
1848      * @param  anObject
1849      *         The object to compare this {@code String} against
1850      *
1851      * @return  {@code true} if the given object represents a {@code String}
1852      *          equivalent to this string, {@code false} otherwise
1853      *
1854      * @see  #compareTo(String)
1855      * @see  #equalsIgnoreCase(String)
1856      */
1857     public boolean equals(Object anObject) {
1858         if (this == anObject) {

  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.ObjectStreamField;
  29 import java.io.UnsupportedEncodingException;
  30 import java.lang.annotation.Native;
  31 import java.lang.foreign.MemorySegment;
  32 import java.lang.foreign.ValueLayout;
  33 import java.lang.invoke.MethodHandles;
  34 import java.lang.constant.Constable;
  35 import java.lang.constant.ConstantDesc;
  36 import java.nio.ByteBuffer;
  37 import java.nio.CharBuffer;
  38 import java.nio.charset.*;
  39 import java.util.ArrayList;
  40 import java.util.Arrays;
  41 import java.util.Comparator;
  42 import java.util.Formatter;
  43 import java.util.List;
  44 import java.util.Locale;
  45 import java.util.Objects;
  46 import java.util.Optional;
  47 import java.util.Spliterator;
  48 import java.util.function.Function;
  49 import java.util.regex.Pattern;
  50 import java.util.regex.PatternSyntaxException;
  51 import java.util.stream.Collectors;
  52 import java.util.stream.IntStream;

1821      }
1822 
1823     /**
1824      * Encodes this {@code String} into a sequence of bytes using the
1825      * {@link Charset#defaultCharset() default charset}, storing the result
1826      * into a new byte array.
1827      *
1828      * <p> The behavior of this method when this string cannot be encoded in
1829      * the default charset is unspecified.  The {@link
1830      * java.nio.charset.CharsetEncoder} class should be used when more control
1831      * over the encoding process is required.
1832      *
1833      * @return  The resultant byte array
1834      *
1835      * @since      1.1
1836      */
1837     public byte[] getBytes() {
1838         return encode(Charset.defaultCharset(), coder(), value);
1839     }
1840 
1841     boolean bytesCompatible(Charset charset) {
1842         if (isLatin1()) {
1843             if (charset == ISO_8859_1.INSTANCE) {
1844                 return true; // ok, same encoding
1845             } else if (charset == UTF_8.INSTANCE || charset == US_ASCII.INSTANCE) {
1846                 return !StringCoding.hasNegatives(value, 0, value.length); // ok, if ASCII-compatible
1847             }
1848         }
1849         return false;
1850     }
1851 
1852     void copyToSegmentRaw(MemorySegment segment, long offset) {
1853         MemorySegment.copy(value, 0, segment, ValueLayout.JAVA_BYTE, offset, value.length);
1854     }
1855 
1856     /**
1857      * Compares this string to the specified object.  The result is {@code
1858      * true} if and only if the argument is not {@code null} and is a {@code
1859      * String} object that represents the same sequence of characters as this
1860      * object.
1861      *
1862      * <p>For finer-grained String comparison, refer to
1863      * {@link java.text.Collator}.
1864      *
1865      * @param  anObject
1866      *         The object to compare this {@code String} against
1867      *
1868      * @return  {@code true} if the given object represents a {@code String}
1869      *          equivalent to this string, {@code false} otherwise
1870      *
1871      * @see  #compareTo(String)
1872      * @see  #equalsIgnoreCase(String)
1873      */
1874     public boolean equals(Object anObject) {
1875         if (this == anObject) {
< prev index next >