[Java] Java was said to be okay to concatenate strings with +, so I checked

What I tried

Make a file like this

PlainString.java


import java.time.LocalDateTime;

class PlainString {
    String getString() {
        //e! String+I will connect with! ??
        return "abc" + LocalDateTime.now();
    }
}

Compile

javac PlainString.java

For JDK8

result

I tried decompiling.

javap -c PlainString
Compiled from "PlainString.java"
class PlainString {
  PlainString();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  java.lang.String getString();
    Code:
       0: new           #2                  // class java/lang/StringBuilder
       3: dup
       4: invokespecial #3                  // Method java/lang/StringBuilder."<init>":()V
       7: ldc           #4                  // String abc
       9: invokevirtual #5                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      12: invokestatic  #6                  // Method java/time/LocalDateTime.now:()Ljava/time/LocalDateTime;
      15: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
      18: invokevirtual #8                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      21: areturn
}

It was firmly replaced by StringBuilder.

Conclusion

~~ Imadoki's Java seems to be okay to concatenate strings using + without worrying about it. ~~ ** Added on 2020/2/22 ** We received a comment from @ cfm-art. Thank you very much. If you just want to concatenate, + is fine. If you use + in a loop, StringBuilder will be created each time, so it's useless.

environment

[Updated on February 22, 2020] For JDK13

I was told that StringBuilder is not used after JDK11, so I tried it.

result

I did javac and then javap.

javap -c PlainString                                                                                                      Compiled from "PlainString.java"
class PlainString {
  PlainString();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  java.lang.String getString();
    Code:
       0: invokestatic  #7                  // Method java/time/LocalDateTime.now:()Ljava/time/LocalDateTime;
       3: invokedynamic #13,  0             // InvokeDynamic #0:makeConcatWithConstants:(Ljava/time/LocalDateTime;)Ljava/lang/String;
       8: areturn
}

The StringBuilder disappeared and was replaced by something called makeConcatWithConstants.

Conclusion

It seems okay to use + anywhere for string concatenation.

environment

openjdk version "13.0.2" 2020-01-14 OpenJDK Runtime Environment AdoptOpenJDK (build 13.0.2+8) OpenJDK 64-Bit Server VM AdoptOpenJDK (build 13.0.2+8, mixed mode, sharing)

Recommended Posts

[Java] Java was said to be okay to concatenate strings with +, so I checked
I tried to interact with Java
How to concatenate strings in java
Java9 was included, so I tried jshell.
I want to use java8 forEach with index
I was addicted to doing onActivityResult () with DialogFragment
I tried to break a block with java (1)
When I tried to unit test with IntelliJ, I was told "java.lang.OutOfMemoryError: Java heap space"
I tried to implement TCP / IP + BIO with JAVA
[Java 11] I tried to execute Java without compiling with javac
I tried to implement Stalin sort with Java Collector
I want to transition screens with kotlin and java!
I want to get along with Map [Java beginner]
I used to make nc (netcat) with JAVA normally
Java: A story that made me feel uncomfortable when I was taught to compare strings with equals for no reason.
How to concatenate strings
I tried to create a java8 development environment with Chocolatey
I tried to modernize a Java EE application with OpenShift.
parquet-tools gives java.lang.ExceptionInInitializerError, so I made it work with java8
I was addicted to setting default_url_options with Rails devise introduction
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
What I was addicted to with the Redmine REST API
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
Several ways to concatenate strings separated by commas in Java
I want to implement various functions with kotlin and java!
Be sure to compare the result of Java compareTo with 0
[Java] I want to test standard input & standard output with JUnit
I tried to be able to pass multiple objects with Ractor
Java to play with Function
Sort strings functionally with java
Connect to DB with Java
Connect to MySQL 8 with Java
A story that I struggled to challenge a competition professional with Java
I want to return to the previous screen with kotlin and java!
Java arguments to run with gradle run can be specified with --args (since Gradle 4.9)
I want to operate cron with GUI, so I will install Dkron
What I was addicted to when implementing google authentication with rails
I checked because the response was strange when debugging with Tomcat 8
I tried to make an Android application with MVC now (Java)
Determine if the strings to be compared are the same in Java
[Java] I want to perform distinct with the key in the object
The operator that was born to be born, instanceof (Java) ~ How to use the instanceof operator ~
I want to extract between character strings with a regular expression
Introduction to Java that can be understood even with Krillin (Part 1)
I tried to solve the past 10 questions that should be solved after registering with AtCoder in Java