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 toHexString() method of the Integer class?
- What is a Java applet?
- What is the floatValue() method?
- What is the purpose of the java.util package?
- What is the purpose of the javax.swing package?