How do I concatenate two strings in Java?

test picture test · Sep 20, 2010 · Viewed 431.4k times · Source

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 . "!");
    }
}

Answer

Peter Lang picture Peter Lang · Sep 20, 2010

You can concatenate Strings using the + operator:

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

theNumber is implicitly converted to the String "42".