Help us understand the problem. What is going on with this article?

[Java] StringとStringBuilderのややこしいところ

More than 1 year has passed since last update.


System.out.println(new StringBuilder("abc")); //=> abc
System.out.println(new StringBuilder("abc").toString()); //=> abc

System.out.println("abc".equals(new StringBuilder("abc"))); //=> false
System.out.println("abc".equals(new StringBuilder("abc").toString())); //=> true


     * Compares this string to the specified object.  The result is {@code
     * true} if and only if the argument is not {@code null} and is a {@code
     * String} object that represents the same sequence of characters as this
     * object.
     * <p>For finer-grained String comparison, refer to
     * {@link java.text.Collator}.
     * @param  anObject
     *         The object to compare this {@code String} against
     * @return  {@code true} if the given object represents a {@code String}
     *          equivalent to this string, {@code false} otherwise
     * @see  #compareTo(String)
     * @see  #equalsIgnoreCase(String)
    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        if (anObject instanceof String) {
            String aString = (String)anObject;
            if (coder() == aString.coder()) {
                return isLatin1() ? StringLatin1.equals(value, aString.value)
                                  : StringUTF16.equals(value, aString.value);
        return false;

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away