The + operator joins two strings together. If either operand is String, the other is converted to String and concatenated with it. This is a common way to convert numbers to Strings.
If a non-String object is concatenated with a String, its toString() method is called. When you write a class that might have a sensible representation as a string, it's useful for debugging purposes to override Object's useless toString() method.
| "abc" + "def" | "abcdef" |
| "abc" + 4 | "abc4" |
| "1" + 2 | "12" |
| "xyz" + (2+2 == 4) | "xyztrue" |
| 1 + "2.5" | "12.5" |
No comments:
Post a Comment