String concatenation implementation in Java 9

Guide on String concatenation implementation in Java 9 with code examples 
17 October 2017   3573

For a start, let's see how string concatenation was implemented in old versions. The "old" way output a bunch of StringBuilder-oriented operations. Let's check this code:

public class Example {
    public static void main(String[] args)
        String result = args[0] + "-" + args[1] + "-" + args[2];

If we compile that with JDK 8 or earlier and then use javap -c Example to see the bytecode, we see something like this:

public class Example {
  public Example();
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
       0: new           #2                  // class java/lang/StringBuilder
       3: dup
       4: invokespecial #3                  // Method java/lang/StringBuilder."<init>":()V
       7: aload_0
       8: iconst_0
       9: aaload
      10: invokevirtual #4                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      13: ldc           #5                  // String -
      15: invokevirtual #4                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      18: aload_0
      19: iconst_1
      20: aaload
      21: invokevirtual #4                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      24: ldc           #5                  // String -
      26: invokevirtual #4                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      29: aload_0
      30: iconst_2
      31: aaload
      32: invokevirtual #4                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      35: invokevirtual #6                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      38: astore_1
      39: getstatic     #7                  // Field java/lang/System.out:Ljava/io/PrintStream;
      42: aload_1
      43: invokevirtual #8                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      46: return

As you can see, it creates a StringBuilder and uses append. This is famous fairly inefficient as the default capacity of the built-in buffer in StringBuilder is only 16 chars, and there's no way for the compiler to know to allocate more in advance, so it ends up having to reallocate. It's also a bunch of method calls. 

Let's look at what Java 9 generates:

public class Example {
  public Example();
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
       0: aload_0
       1: iconst_0
       2: aaload
       3: aload_0
       4: iconst_1
       5: aaload
       6: aload_0
       7: iconst_2
       8: aaload
       9: invokedynamic #2,  0              // InvokeDynamic #0:makeConcatWithConstants:(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
      14: astore_1
      15: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
      18: aload_1
      19: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      22: return

It makes a single call to makeConcatWithConstants from StringConcatFactory


Java SE 14 to be Available

Java SE 14 is as a regular support period version for which updates will be released before the next release
18 March 2020   172

After six months of development, Oracle released the Java SE 14 (Java Platform, Standard Edition 14), which uses the OpenJDK open source project as its reference implementation. Java SE 14 maintains backward compatibility with previous releases of the Java platform; all previously written Java projects will work without changes when launched under the new version. Ready-to-install Java SE 14 builds (JDK, JRE, and Server JRE) are prepared for Linux (x86_64), Windows, and macOS. The Java 14 reference implementation developed by the OpenJDK project is fully open under the GPLv2 license with GNU ClassPath exceptions that allow dynamic linking to commercial products.

Java SE 14 is categorized as a regular support period for which updates will be released before the next release. As a branch with a long service life (LTS), you should use Java SE 11, updates for which will be released until 2026. The previous Java 8 LTS branch will be supported until December 2020. The next LTS release is scheduled for September 2021. Recall that since the release of Java 10, the project has switched to a new development process, which implies a shorter cycle of generating new releases. New functionality is now being developed in one constantly updated master branch, in which ready-made changes are included and from which branches are released every six months to stabilize new releases.

These are some of the changes and updates:

  • Added experimental support for pattern matching in the instanceof operator, which allows you to immediately determine the local variable to access the checked value.
  • Experimental support has been added for the new “record” keyword, which provides a compact form for defining classes, avoiding the explicit definition of various low-level methods, such as equals (), hashCode () and toString (), in cases where data is stored only in fields, the behavior of work with which does not change.
  • This declaration will automatically add implementations of the equals (), hashCode (), and toString () methods in addition to the constructor and methods that control the change of data (getter).
  • Standardized and enabled by default is support for a new form of switch statements that does not require a break statement, allows you to combine duplicate labels, and allows use not only in the form of an operator, but also as an expression.
  • The experimental support for text blocks has been expanded - a new form of string literals that allows you to include multiline text data in the source code without using character escaping and preserving the original text formatting in the block
  • The informative value of diagnostics in case of NullPointerException exceptions has been expanded.
  • A preliminary version of the jpackage utility has been implemented, which allows you to create packages for self-contained Java applications.
  • A new memory allocation mechanism has been added to the G1 garbage collector, taking into account the specifics of working on large systems using the NUMA architecture.
  • Added API for tracking on-the-fly JFR events (JDK Flight Recorder), for example, for organizing continuous monitoring.
  • Added the jdk.nio.mapmode module, which offers new modes (READ_ONLY_SYNC, WRITE_ONLY_SYNC) for creating mapped byte buffers (MappedByteBuffer) that reference non-volatile memory (NVM).
  • A preliminary version of the Foreign-Memory Access API has been implemented, which allows Java applications to safely and efficiently access memory areas outside the Java heap by manipulating new abstractions of MemorySegment, MemoryAddress, and MemoryLayout.
  • Ports for Solaris OS and SPARC processors (Solaris / SPARC, Solaris / x64 and Linux / SPARC) declared obsolete with intent to delete.

Get more at the Oracle website.