14.3.6 Concatenating Strings

String method concat (Fig. 14.7) concatenates two String objects (similar to using the + operator) and returns a new String object containing the characters from both original Strings. The expression s1.concat(s2) at line 13 forms a String by appending the characters in s2 to the those in s1. The original Strings to which s1 and s2 refer are not modified.

 1   // Fig. 14.7: StringConcatenation.java 2   // String method concat. 3  4   public class StringConcatenation 5   { 6      public static void main(String[] args) 7      { 8         String s1 = "Happy "9         String s2 = "Birthday";10 11         System.out.printf("s1 = %s%ns2 = %s%n%n",s1, s2);12         System.out.printf(

Get Java™ How To Program (Early Objects), Tenth Edition 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.