Skip to main content
Lesson 13 - String Class
ZIPPDF (letter)
Lesson MenuPreviousNext
  
The String Class page 3 of 12

  1. Character strings in Java are not represented by primitive types as are integers (int) or single characters (char). Strings are represented as objects of the String class. The String class is defined in java.lang.String, which is automatically imported for use in every program you write. We've used character string literals, such as "Enter a value" in earlier examples. Now we can begin to explore the String class and the capabilities that it offers.

  2. A String is the only Java reference data type that has a built-in syntax for constants. These constants, referred to as string literals, consist of any sequence of characters enclosed within double quotations. For example:

    "This is a string"
    "Hello World!"
    "\tHello World!\n"

    The characters that a String object contains can include control characters. The last example contains a tab (\t) and a linefeed (\n) character, specified with escape sequences.

  3. A second unique characteristic of the String type is that it supports the "+" operator to concatenate two String expressions. For example:

    sentence = "I " + "want " + "to be a " + "Java programmer.";

    The "+" operator can be used to combine a String expression with any other expression of primitive type. When this occurs, the primitive expression is converted to a String representation and concatenated with the string. For example, consider the following instruction sequence:

    PI = 3.14159;
    System.out.prinln("The value of PI is " + PI);
    
    Run output:
    
    The value of PI is 3.14159
    
  4. String shares some of the characteristics with the primitive types, however, String is a reference type, not a primitive type. As a result, assigning one String to another copies the reference to the same String object. Similarly, each String method returns a new String object.

  5. The rest of the student outline details a partial list of the methods provided by the Java String class. For a detailed listing of all of the capabilities of the class, please refer to Sun's Java documentation.


Lesson MenuPreviousNext
Contact
 ©ICT 2003, All Rights Reserved.