String Objects and the + Operator

For convenience in working with strings, Java also uses the + and += operators to indicate concatenation. When the compiler finds an expression in which a string appears in association with a + operator, it turns all items in the expression into strings, concatenates the strings, and creates a new String object. However, remember that expressions are evaluated left to right, so if the compiler sees a numeric operation before the String, it will carry out that operation before the conversion to a String. You can see this in action in the following code, which produces "101 is the result", not "1001 is the result".

int n = 1 ;
System.out.println( 100 + n + " is the result ");

The compiler has a complete set of ...

Get Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035) now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.