DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Immutable Objects Using Record in Java
  • Squid Game: The Clean Code Trials — A Java Developer's Survival Story
  • Writing DTOs With Java8, Lombok, and Java14+
  • Formatting Strings in Java: String.format() Method

Trending

  • PWA on iOS, Part 2: HTTPS, Manifest and Service Worker
  • How to Build Your Own AI SRE From Scratch
  • Why Role-Based Access Control Fails for AI Agents — and How to Fix It With Memory Lineage Engines
  • Giving AI Agents Safe Access to Legacy Systems: Introducing legacy2mcp

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. Concatenating Strings in Java 9

Concatenating Strings in Java 9

By 
Daniela Kolarova
DZone Core CORE ·
May. 19, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
13.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Have you ever had the need to build a comma-separated list of values from a list or an array in Java or wanted to create a file path using concatenated folder names and a file path delimiter? Well, in the article Cocatenating Strings in Java 8, I have described a convinient way to use Java 8 APIs to do the job.

This not only reduces your development time and prevents additional errors, but it also let's you write more readable and understandable code. But what exactly happened since Java 9 was released, and does it apply to the code presented in the cases described in the aforementioned article.

When you implemented your first application in Java or read some Java books and articles, you probably noticed that you shouldn’t concatenate Strings with + operator. And that’s correct if you’re concatenating Strings in your application logic. 

Strings are immutable, and the result of each String concatenation is stored in a new String object. That requires additional memory and slows down your application, especially if you’re concatenating multiple Strings within a loop. Experienced Java programmers know that using a class StringBuffer to append String values was the preferred approach, and as of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, called StringBuilder. StringBuilder should generally be used in preference to StringBuffer, as it supports all of the same operations, but it is faster, as it performs no synchronization. 

Now, let us revisit an example from my previous article:

Java
xxxxxxxxxx
1
 
1
String baseDirectory = "baseDir";
2
String subFolder = "subFolder";
3
String fileName = "fileName";
4
5
List<String> filePathParts = Arrays.asList(baseDirectory, subFolder, fileName);
6
File file = new File(String.join(File.separator, filePathParts));


When the String's join method is called, it delegates the calls to a slightly more sophisticated class, called StringJoiner, which in fact offers more than just concatenating strings with a parametrized delimiter. As the Javadoc states:

StringJoiner is used to construct a sequence of characters separated
by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.

In Java 8, the StringJoiner implementation is optimized for Strings concatenation and is based on the usage of StringBuilder. By using the Java API for building comma-separated values, we were sure that our code was optimized for the use case of having a lot of values that have to be concatenated in on String value. The integration of the StringJoiner in Java Collectors API for Streams was a huge benefit for us as well. But since then, Java 9 changed the game.

In Java 9, each String concatenation is translated into a call to invokedynamic, as described in JDK Enhancement Proposal 280. JEP 280 changed the static String-concatenation bytecode sequence generated by javac to use invokedynamic calls to JDK library functions. This enables future optimizations of String concatenation without requiring further changes to the bytecode emitted by Java. 

What this generally means is that concatenating String values with the + operator turns out to be faster when compared to using StringBuilder, which was earlier used in the Java 8 bytecode when simple + concatenation optimizations were applied.

But what happens to the legacy code we have already written like the one below, based on StringJoiner and Collectors for String concatenation:

Java
xxxxxxxxxx
1
 
1
String suffix  = new SimpleDateFormat("yyyyMMdd").format(new Date());
2
String paramValue = "P";
3
List<String> filePathParts = Arrays.asList(baseDirectory, subFolder, "%s", fileName);
4
String filePath = filePathParts.stream().map(value -> String.format(value, paramValue))
5
          .collect(Collectors.joining(File.separator, "", suffix));
6
7
>>Output: baseDir\subFolder\P\fileName20170804


Well, the StringJoiner class was also changed since Java 9 and does not use the StringBuilder any more. Instead of storing the values in a StringBuilder, an array of String values is used, and the concatenation is based on the method, getChars, provided by String:

Java
x
 
1
getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)


This change is just an implementation detail that does not directly relate to the Java 9 String concatenation changes. As a matter of fact, for huge lists with millions of elements, the new version performs better, as it does not depend on a Java class (StringBuilder) but on native methods invoked in the String class implementation. 

The good news is the Java API for building comma-separated values is still valid and has been improved since Java 9. More importantly, it makes sense to rely on the Java APIs instead of using custom implementations or external libraries because moving to newer Java versions might optimize your legacy code without the need to rewrite, deploy, and publish any new releases of your old code.

Java (programming language) Strings

Opinions expressed by DZone contributors are their own.

Related

  • Immutable Objects Using Record in Java
  • Squid Game: The Clean Code Trials — A Java Developer's Survival Story
  • Writing DTOs With Java8, Lombok, and Java14+
  • Formatting Strings in Java: String.format() Method

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook