30.3. Class String

Class String is used to represent strings in Java. The next several subsections cover many of class String’s capabilities.

30.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. 30.1.

Figure 30.1. String class constructors.
 1 // Fig. 30.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 ...

Get Java™ How to Program, Seventh 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.