How do I concatenate two strings in Java?

前端 未结 23 3084
长情又很酷
长情又很酷 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 04:58

    This should work

    public class StackOverflowTest
    {  
        public static void main(String args[])
        {
            int theNumber = 42;
            System.out.println("Your number is " + theNumber + "!");
        }
    }
    
    0 讨论(0)
  • 2020-11-22 04:59
    import com.google.common.base.Joiner;
    
    String delimiter = "";
    Joiner.on(delimiter).join(Lists.newArrayList("Your number is ", 47, "!"));
    

    This may be overkill to answer the op's question, but it is good to know about for more complex join operations. This stackoverflow question ranks highly in general google searches in this area, so good to know.

    0 讨论(0)
  • 2020-11-22 05:00

    "+" not "."

    But be careful with String concatenation. Here's a link introducing some thoughts from IBM DeveloperWorks.

    0 讨论(0)
  • 2020-11-22 05:00

    First method: You could use "+" sign for concatenating strings, but this always happens in print. Another way: The String class includes a method for concatenating two strings: string1.concat(string2);

    0 讨论(0)
  • 2020-11-22 05:01

    Use + for string concatenation.

    "Your number is " + theNumber + "!"
    
    0 讨论(0)
  • 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(<primitive-type>).

    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.

    0 讨论(0)
提交回复
热议问题