[Java] The confusing part of String and StringBuilder

Comparison of String and StringBuilder


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

Because String and StringBuilder are not inherited or implemented, the equals method of the String class is overridden as follows.


/**
     * 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;
    }

Recommended Posts

[Java] The confusing part of String and StringBuilder
I tried to summarize the methods of Java String and StringBuilder
[Java] About String and StringBuilder
[Java] Handling of character strings (String class and StringBuilder class)
[Java] Cut out a part of the character string with Matcher and regular expression
The story of low-level string comparison in Java
[Java] String comparison and && and ||
[Java] Get the length of the surrogate pair string
[Note] Java: Measures the speed of string concatenation
I compared the characteristics of Java and .NET
Please note the division (division) of java kotlin Int and Int
The comparison of enums is ==, and equals is good [Java]
Organizing the current state of Java and considering the future
Java language from the perspective of Kotlin and C #
Until the use of Spring Data and JPA Part 2
[Java] About Objects.equals () and Review of String comparisons (== and equals)
Until the use of Spring Data and JPA Part 1
I summarized the types and basics of Java exceptions
Replace only part of the URL host with java
Java and Iterator Part 1 External Iterator
Apache Hadoop and Java 9 (Part 1)
Advantages and disadvantages of Java
[Note] Java Output of the sum of odd and even elements
I didn't understand the behavior of Java Scanner and .nextLine ().
JSON in Java and Jackson Part 1 Return JSON from the server
I tried to summarize the basics of kotlin and java
Command to check the number and status of Java threads
The nth and n + 1st characters of a Ruby string
Java beginners briefly summarized the behavior of Array and ArrayList
[Java] Try editing the elements of the Json string using the library
[Java] Delete the specified number of characters from the end of StringBuilder
[Introduction to Java] Handling of character strings (String class, StringBuilder class)
[Java] I thought about the merits and uses of "interface"
[Java] Speed comparison of string concatenation
[Java] Delete the elements of List
Qualify only part of the text
Various methods of Java String class
About fastqc of Biocontainers and Java
Basic usage of java Optional Part 1
This and that of the JDK
StringBuffer and StringBuilder Class in Java
Various methods of the String class
[Java] Judgment of identity and equivalence
Studying Java 8 (String Joiner and join)
[Java] Correct comparison of String type
I read the source of String
[Java] Difference between StringBuffer and StringBuilder
The origin of Java lambda expressions
[Java] Various summaries attached to the heads of classes and members
[Java] Get the dates of the past Monday and Sunday in order
The story of not knowing the behavior of String by passing Java by reference
About the relationship between the Java String equality operator (==) and initialization. Beginners
Read the first 4 bytes of the Java class file and output CAFEBABE
[Java] Appropriate introduction by the people of Tempa Java Part 0 (Code rules)
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
[Java] How to convert from String to Path type and get the path
How to check for the contents of a java fixed-length string
Let's implement the condition that the circumference and the inside of the Ougi shape are included in Java [Part 2]
Let's implement the condition that the circumference and the inside of the Ougi shape are included in Java [Part 1]
Get the result of POST in Java
Check the contents of the Java certificate store