[Java] About String and StringBuilder

Summary with lottery

This is a memo for yourself for studying

--About the difference between String and StringBuilder in the first place --About stringsbuilder equals

The conclusion is --StringBuilder is recommended when joining and flipping --If not, String may be more convenient & safe --Be careful about string match judgment with String and StringBuilder!

background

Recently, I am studying Java grammar using the AtCoder problem.

ABC146 F -Sugoroku It is a story that starts when I came across something called StringBuilder when I wanted to invert String when I came across.

Difference between String and StringBuilder

You can easily understand this by looking at the link below. https://qiita.com/shunsuke227ono/items/e8f34c67dcffa0fa28ad https://www.javadrive.jp/start/stringbuilder/

To summarize briefly

--String is immutable and StringBuilder is mutable --String can also be described as mutable in the code (S = S + "a" etc.) --StringBuilder is more memory and time efficient for adding and inserting strings

So it seems that the use of StringBuilder is recommended, but ** I didn't see any advantages on the String side **, so that? Is it a child who doesn't need String? (Probably not).

――― 2020/02/02 postscript ――― (The title has also been changed) He pointed out in the comments and introduced ↓ good links ↓. https://qiita.com/yoshi389111/items/67354ba33f9271ef2c68

There seems to be no problem with the + operator when combining string constants or when combining with one statement. If you want to invert the string like this time, it seems better to use StringBuilder or List \ <Character >.

Addendum 2 (2020/03/27)

Added because I encountered a different problem and knowledge

ABC159 B - String Palindrome Judgment whether the character string is a palindrome (+ α). I wrote the code with the idea of whether a StringBuilder and its inverted version are the same.

Palindrome judgment code (wrong)


static boolean checkKaibun(String str){
  StringBuilder t = new StringBuilder(str);
  return (t.equals(t.reverse()));
}

This always returns true as far as I've tried it myself. The cause is that the StringBuilder's equals method has not been overridden.

The solution is

  1. Override the equals method
  2. Convert to String with toString and then compare

If you use a lot of StringBuilder comparisons in your code, it would be 1, but it seems that you also need to override the hashCode method (see below), which is annoying.

reference https://www.atmarkit.co.jp/ait/articles/0702/20/news103.html

The implementation in 2 is as follows.

Palindrome judgment code


static boolean checkKaibun(String str){
  StringBuilder t = new StringBuilder(str);
  return (str.equals(t.reverse().toString()));
}

Recommended Posts

[Java] About String and StringBuilder
About Java StringBuilder class
[Java] String comparison and && and ||
About Java String class
[Java] The confusing part of String and StringBuilder
About Java Packages and imports
Java string
[Java] About Objects.equals () and Review of String comparisons (== and equals)
[Java] Handling of character strings (String class and StringBuilder class)
About Java static and non-static methods
About fastqc of Biocontainers and Java
[Java beginner] About abstraction and interface
StringBuffer and StringBuilder Class in Java
About Java primitive types and reference types
Studying Java 8 (String Joiner and join)
This and that about Base64 (Java)
[Java] Difference between StringBuffer and StringBuilder
About Java interface
[Java] About arrays
[Java] String padding
Something about java
Where about java
About Java features
About Java threads
[Java] About interface
[About JDBC that connects Java and SQL]
Java string processing
About Java class
About the relationship between the Java String equality operator (==) and initialization. Beginners
[Java] Collection and StringBuilder operation method comparison
Java and JavaScript
About Java arrays
XXE and Java
About java inheritance
I tried to summarize the methods of Java String and StringBuilder
About interface, java interface
Split string (Java)
About List [Java]
About java var
About Java literals
About Java commands
Unexpectedly unknown basic tips about StringBuilder [Java / C #]
About Java log output
About Java functional interface
Java, about 2D arrays
About the StringBuilder class
About class division (Java)
About [Java] [StreamAPI] allMatch ()
Getters and setters (Java)
[Java] Thread and Runnable
String and stringbuffer and string builder
Java true and false
[Java] About Singleton Class
String literals and instances
About classes and instances
Java string multiple replacement
[Java] About anonymous classes
About method splitting (Java)
About gets and gets.chomp
[Java Silver] About initialization
About Java Array List