[java] How do I concatenate two strings in Java?

String.join( delimiter , stringA , stringB , … )

As of Java 8 and later, we can use String.join.

Caveat: You must pass all String or CharSequence objects. So your int variable 42 does not work directly. One alternative is using an object rather than primitive, and then calling toString.

Integer theNumber = 42;
String output = 
    String                                                   // `String` class in Java 8 and later gained the new `join` method.
    .join(                                                   // Static method on the `String` class. 
        "" ,                                                 // Delimiter.
        "Your number is " , theNumber.toString() , "!" ) ;   // A series of `String` or `CharSequence` objects that you want to join.
    )                                                        // Returns a `String` object of all the objects joined together separated by the delimiter.
;

Dump to console.

System.out.println( output ) ;

See this code run live at IdeOne.com.