+ Operator

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

Yes: in output statements like this:

System.out.println( "Result is:" + result );

The + operator is a short way of asking for concatenation. (If result is a number, it is first converted into characters before the concatenation is done.)

+ Operator

concatenation in action

 

Here the + operator is used instead of using the concat() method:

String first = "Red " ;
String last  = "Rose" ;
String name  = first + last ;

String concatenation, done by concat() or by +, always constructs a new object based on data in other objects. Those objects are not altered at all.

When the operands on either side of + are numbers, then + means "addition". If one or both of the operands is a String reference, then String concatenation is performed. When an operator such as + changes meaning depending on its arguments, it is said to be overloaded.

QUESTION 11:

Say that the following statement is added after the others:

String reversed = last + first;

Does it change first, last, or name?