What does "+" mean in Java?



Occasionally I may include affiliate links, which means I may get a commision if you purchase something via that link. Check out my privacy policy for more info.

This is my answer to this question on Quora.

When dealing with numbers, it adds them like you’d expect.

When dealing with string, it concatenates them. That is, puts one after another into another string.

For example:

1
2
3
4
5
6
7
int x = 5 + 5;
String s = "Me" + "you";
String s2 = "Me" + x + "you";

System.out.println(x);
System.out.println(s);
System.out.println(s2);

This outputs the following:

10
Meyou
Me10you

Now try to figure what this outputs:

1
2
String s3 = "Me" + x + 5 + "you";
System.out.println(s3);

Leave a Reply

Note that comments won't appear until approved.