Out of the box, you have 3 ways to insert the variable value into a String as you are trying to achieve:
1. The easiest way
You can simply use the + operator between String and any type of object or primitive, it will automatically combine String and
- In the case of an object, the value of
String.valueOf(obj) corresponding String " null " if obj is null otherwise the value is obj.toString() . - In the case of a primitive type, the equivalent of
String.valueOf(<primitive-type>) .
An example with a non- null object:
Integer theNumber = 42; System.out.println("Your number is " + theNumber + "!");
Exit:
Your number is 42!
Example with a null object:
Integer theNumber = null; System.out.println("Your number is " + theNumber + "!");
Exit:
Your number is null!
Example with a primitive type:
int theNumber = 42; System.out.println("Your number is " + theNumber + "!");
Exit:
Your number is 42!
2. Explicit way and potentially the most effective
You can use StringBuilder (or StringBuffer thread-safe legacy counterpart) to build your String using the append methods.
Example:
int theNumber = 42; StringBuilder buffer = new StringBuilder() .append("Your number is ").append(theNumber).append('!'); System.out.println(buffer.toString());
Exit:
Your number is 42!
Behind the scenes, this is actually how recent Java compilers convert all String concatenation done with the + operator, the only difference with the previous method is that you have full control.
Indeed, compilers will use the default constructor , therefore the default capacity is ( 16 ), since they have no idea what the final length of the String will be to build, which means that if the final length is greater than 16 , the capacity will be necessarily extended, which has a price in performance plan.
Therefore, if you know in advance that the size of your final String will be greater than 16 , it will be much more efficient to use this approach to provide better initial capacity. For example, in our example, we create a String whose length is greater than 16, so for best performance it should be rewritten as follows:
The example is optimized:
int theNumber = 42; StringBuilder buffer = new StringBuilder(18) .append("Your number is ").append(theNumber).append('!'); System.out.println(buffer)
Exit:
Your number is 42!
3. The most read way
You can use the String.format(locale, format, args) or String.format(format, args) which both use Formatter to create your String . This allows you to specify the format of your final String , using placeholders that will be replaced by the value of the arguments.
Example:
int theNumber = 42; System.out.println(String.format("Your number is %d!", theNumber)); // Or if we need to print only we can use printf System.out.printf("Your number is still %d with printf!%n", theNumber);
Exit:
Your number is 42! Your number is still 42 with printf!
The most interesting aspect of this approach is the fact that we have a clear idea of ββwhat will be the last String because it is much easier to read, and it is much easier to maintain.