String Interpolation

To use this mechanism is enough to put "+" operator in front of a string literal. Then it is possible to use any valid Java expressions inside of the string using ${expression} syntax. See example below for details.

Vanilla Java


      class SomeClass {

          SomeFactory someFactory;

          // [...]

          String generateNameAddingStrings(String component, int postfix) {
              return "specificPrefix " + component + " (" + someFactory.uniqueIdentifier() + ") " + postfix;
          }

          String generateNameWithStringFormat(String component, int postfix) {
              return String.format("specificPrefix %s (%s) %d", component, someFactory.uniqueIdentifier(), postfix);
          }

          String generateNameWithStringBuilder(String component, int postfix) {
              return new StringBuilder("specialPrefix ").append(component).append(" (")
                            .append(someFactory.uniqueIdentifier()).append(") ").append(postfix).toString();
          }

      }
  

Kendal


      class SomeClass {

          SomeFactory someFactory;

          // [...]

          String generateNameWithStringInterpolation(String component, int postfix) {
              return +"specificPrefix ${component} (${someFactory.uniqueIdentifier()}) ${postfix}";
          }

      }