The concat() Method

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.

The concat() Method

The parts of the statement match the documentation correctly:

String name = first.concat( last ); 
 ----+----    --+-- --+--  --+--
     |          |     |      |
     |          |     |      |
     |          |     |      +---- a String reference parameter
     |          |     |
     |          |     +----- the name of the method
     |          |
     |          +----- dot notation used to call an object's method.
     |
     +----- the method returns a reference to a new String object

The concat method performs String concatenation. A new String is constructed using the data from two other Strings. In the example, the first two Strings (referenced by first and last) supply the data that concat() uses to construct a third String (referenced by name.)

String first = "Red " ;
String last  = "Rose" ;
String name  = first.concat( last );

The first two Strings are NOT changed by the action of concat(). A new String is constructed that contains the characters "Red Rose".

Concat() Method Call

The picture shows the operation of the concat() method before the reference to the new object has been assigned to name.

QUESTION 10:

(Review:) Have you seen String concatenation before?