String concatenation implementation in Java 9

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

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


Apache NetBeans 11.2 IDE to be Released

The release contains support for the Java SE, Java EE, PHP, JavaScript, and Groovy programming languages
05 November 2019   170

Organization The Apache Software Foundation introduced the Apache NetBeans 11.2 IDE. This is the fourth release prepared by the Apache Foundation after the transfer of NetBeans code by Oracle and the first release after moving a project from an incubator to the category of primary Apache projects. The release contains support for the Java SE, Java EE, PHP, JavaScript, and Groovy programming languages. The transfer of C / C ++ support from the code base provided by Oracle is expected in release 11.3, scheduled for January. In April 2020, the release of Apache NetBeans 12 will be formed, which will be accompanied as part of an extended support cycle (LTS).

Key NetBeans 11.2 innovations:

  • Added support for Java SE 13. For example, the ability to use "switch" in the form of an expression rather than an operator is added. Highlighting and conversion operations are implemented for text blocks that include multi-line text data without using character escaping in them and preserving the original text formatting. The indicated features have so far been marked as test and are activated only during assembly with the "--enable-preview" flag;
  • Added new features of the PHP language, developed in the 7.4 branch, the release of which is scheduled for November 28. NetBeans added processing of such innovations as typed properties, the operator "?? =" ("a ?? = b" is similar to "a = a ?? b"), the ability to substitute existing arrays when defining a new array (operator "... $ var "), a new mechanism for serializing objects (a combination of Serializable and __sleep () / __ wakeup ()), the ability to visualize large numbers (1_000_000_00) and a new format for defining functions" fn (parameter_list) => expr "(for example," fn ($ x ) => $ x + $ y "is analogous to" $ fn2 = function ($ x) use ($ y) {return $ x + $ y;} ").
  • Performance optimizations are carried out: The search speed of binary files in the tree with source texts has been increased. On Linux and Windows, the WatchService interface provided by the Java NIO2 API is used to track changes to directories. Faster identification of files with archives;
  • Improved support for the Gradle build system. Added the ability to load Java compiler flags, which allows you to use Java experimental features in Gradle projects ("it.options.compilerArgs.add ('- enable-preview')"). Also added is the processing of user input in a tab that reflects the progress of the assembly (Output). When starting the Gradle Daemon background process, the org.gradle.jvmargs property is now taken into account;
  • The problems with the license for the code with the JavaScript parser were resolved due to which the parser had to be installed separately earlier. Now the graal-js parser has been transferred from the GPL to UPL (Universal Permissive License);
  • Installer work has been improved, in which support for the custom installation of individual NetBeans components has appeared;
  • Payara application server support updated to Payara Platform 5.193;
  • Added support for the extended attribute syntax in HTML5 used in Angular (for example, <input [value] = 'test' />, <input (change) = 'test' />, etc.)