[{"data":1,"prerenderedAt":15},["ShallowReactive",2],{"$f37ezovv3vyrbl":3},{"slug":4,"title":5,"date":6,"excerpt":7,"tags":8,"source":11,"aiAssisted":12,"readingTime":13,"html":14,"disabled":12},"java-string-interpolation-jep-430-2023Oct07","Java String Interpolation - JEP 430","2023-10-07","Exploring Java string interpolation and the String Templates preview in JEP 430",[9,10],"Java","Kotlin","LinkedIn",false,"1 min read","\u003Cp>Interpolation is defined as something that is introduced or inserted.\u003C\u002Fp>\n\u003Cp>String interpolation, a concept in programming languages, means inserting or injecting a variable into a string.\u003C\u002Fp>\n\u003Cp>In Java, there are several ways of achieving string interpolation:\u003C\u002Fp>\n\u003Cul>\n\u003Cli>Concatenation operator, also known as the plus operator\u003C\u002Fli>\n\u003Cli>\u003Ccode>format()\u003C\u002Fcode> function\u003C\u002Fli>\n\u003Cli>\u003Ccode>MessageFormat.format\u003C\u002Fcode>\u003C\u002Fli>\n\u003Cli>\u003Ccode>StringBuilder\u003C\u002Fcode> class\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Cp>According to \u003Ca href=\"https:\u002F\u002Fopenjdk.org\u002Fjeps\u002F430\">JEP 430\u003C\u002Fa>, String Templates offer a new way that was in the Preview state. It offers the \u003Ccode>STR\u003C\u002Fcode> template processor, for example:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-java\">String nameString = STR.&quot;My first is \\{firstName} and last name is \\{lastName}&quot;;\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>I would prefer to use the \u003Ccode>STR\u003C\u002Fcode> template processor implicitly. For example:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-java\">String nameString = &quot;My first is \\{firstName} and last name is \\{lastName}&quot;;\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>JEP 430 states that this was considered but not adopted.\u003C\u002Fp>\n\u003Cp>My preference is also to use \u003Ccode>$\u003C\u002Fcode> instead of \u003Ccode>\\\u003C\u002Fcode>, for example:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-java\">String nameString = &quot;My first is ${firstName} and last name is ${lastName}&quot;;\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>\u003Ca href=\"https:\u002F\u002Fkotlinlang.org\u002F\">Kotlin\u003C\u002Fa>, which I call the sister language of Java, already implements this.\u003C\u002Fp>\n\u003Cp>What are your thoughts?\u003C\u002Fp>\n\u003Cp>\u003Ca href=\"https:\u002F\u002Fwww.linkedin.com\u002Fposts\u002Fkamlesh1808_java-string-interpolation-jep-430-interpolation-activity-7116489386585726976-1ni7\">Originally published on LinkedIn\u003C\u002Fa>.\u003C\u002Fp>\n",1792023078796]