O'Reilly logo

Java™ How to Program, Seventh Edition by P. J. Deitel - Deitel & Associates, Inc., H. M. Deitel - Deitel & Associates, Inc.

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required