Search code examples
javastring-concatenation

How do I concatenate two strings in Java?


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

public class StackOverflowTest {  
    public static void main(String args[]) {
        int theNumber = 42;
        System.out.println("Your number is " . theNumber . "!");
    }
}

Solution

  • You can concatenate Strings using the + operator:

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

    theNumber is implicitly converted to the String "42".