What does "+" mean in Java?
September 15, 2016
This site utilizes Google Analytics, Google AdSense, as well as participates in affiliate partnerships with various companies including Amazon. Please view the privacy policy for more details.
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
Thank you. Your comment will be visible after approval.
Your comment has been submitted, but their seems to be an error. Check your browser console for more details.