Jvm should be able to determine that it is a constant, and optimize it if necessary, so you do not need to worry about any style.
In the case of arithmetic expressions, the JVM can perform continuous flexion optimization.
If you are only interested in strings, constant strings are stored inside the string pool.
As you know, strings in java are immutable.
Therefore, if you have persistent strings in java, they will be stored only once, and all links will point to this string object.
eg.
String s = "a" + "bc"; String t = "ab" + "c"; System.out.println(s == t);
returns true since t and s point to the same string object.
In addition, java classes have constant pools to which all constants like these move.
Essentially, the JVM is pretty smart, and I don't think any of these toy examples are better than any other, at least in terms of efficiency. Although, as the other answers mention, there are software development considerations that you probably should consider.
source share