14.3.1 String Constructors

Class String provides constructors for initializing String objects in a variety of ways. Four of the constructors are demonstrated in the main method of Fig. 14.1.

 1   // Fig. 14.1: StringConstructors.java 2   // String class constructors. 3  4   public class StringConstructors 5   { 6      public static void main(String[] args) 7      { 8         char[] charArray = {'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y'}; 9         String s = new String("hello");10 11         // use String constructors              12         String s1 = new String();               13         String s2 = new String(s);              14         String s3 = new String(charArray);      15         String s4 = new

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.