Home Segments Top Top Previous Next

122: Mainline

You frequently see the + operator, viewed as the concatenation operator, in print statements. Such use of the + operator often enables compact display statements.

For example, you can certainly write an expression such as the following:

System.out.print("The rating of the movie is ");  
System.out.println(s + a + d);  

Alternatively, you can combine the two statements into one, using concatenation to bring the information together:

System.out.print("The rating of the movie is " + (s + a + d));  

Note that the parentheses around the summed variables are essential. If they are absent, Java assumes that you want to concatenate the first string with the value of s, transformed into a string, then a, then d; Java does concatenation, but does no addition.