nn2 nn2 - 4 months ago 6
Java Question

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

Answer

You can concatenate Strings using the + operator:

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

theNumber is implicitly converted to the String "42".