What is the String + operator?
What is the String + operator?
Rating:
The + operator concatenates two strings, producing a new String object as the result. For example,
String s = "John gave me " + amt + "dollars";
System.out.println(s);
This code will display the string "John gave me 50 dollars".
The + operator may also be used to concatenate a string with other data types. For example,
String s = "John gave me" + amt + "dollars";
System.out.println(s);
This code will display the string "John gave me 50 dollars". In this case, the variable amt is declared as int rather than String, but the output produced is the same. This is because the int value contained in the variable amt is automatically converted to String type, and then the + operator concatenates the two strings.
Rating:
Other articles
- What is the FileInputStream class?
- What is an abstract class?
- What is inheritance?
- What is the startsWith() method?
- What is LinkedList?