How do I concatenate two strings in Java?

前端 未结 23 3068
长情又很酷
长情又很酷 2020-11-22 04:47

I am trying to concatenate strings in Java. Why isn\'t this working?

public class StackOverflowTest {  
    public static void main(String args[]) {
                


        
23条回答
  •  礼貌的吻别
    2020-11-22 05:03

    Out of the box you have 3 ways to inject the value of a variable into a String as you try to achieve:

    1. The simplest way

    You can simply use the operator + between a String and any object or primitive type, it will automatically concatenate the String and

    1. In case of an object, the value of String.valueOf(obj) corresponding to the String "null" if obj is null otherwise the value of obj.toString().
    2. In case of a primitive type, the equivalent of String.valueOf().

    Example with a non null object:

    Integer theNumber = 42;
    System.out.println("Your number is " + theNumber + "!");
    

    Output:

    Your number is 42!
    

    Example with a null object:

    Integer theNumber = null;
    System.out.println("Your number is " + theNumber + "!");
    

    Output:

    Your number is null!
    

    Example with a primitive type:

    int theNumber = 42;
    System.out.println("Your number is " + theNumber + "!");
    

    Output:

    Your number is 42!
    

    2. The explicit way and potentially the most efficient one

    You can use StringBuilder (or StringBuffer the thread-safe outdated 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()); // or simply System.out.println(buffer)
    

    Output:

    Your number is 42!
    

    Behind the scene, this is actually how recent java compilers convert all the String concatenations done with the operator +, the only difference with the previous way is that you have the full control.

    Indeed, the compilers will use the default constructor so the default capacity (16) as they have no idea what would be the final length of the String to build, which means that if the final length is greater than 16, the capacity will be necessarily extended which has price in term of performances.

    So 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 a better initial capacity. For instance, in our example we create a String whose length is greater than 16, so for better performances it should be rewritten as next:

    Example optimized :

    int theNumber = 42;
    StringBuilder buffer = new StringBuilder(18)
        .append("Your number is ").append(theNumber).append('!');
    System.out.println(buffer)
    

    Output:

    Your number is 42!
    

    3. The most readable way

    You can use the methods String.format(locale, format, args) or String.format(format, args) that both rely on a Formatter to build your String. This allows you to specify the format of your final String by using place holders 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);
    

    Output:

    Your number is 42!
    Your number is still 42 with printf!
    

    The most interesting aspect with this approach is the fact that we have a clear idea of what will be the final String because it is much more easy to read so it is much more easy to maintain.

提交回复
热议问题