go to previous page   go to home page   go to next page

Answer:

Expression New String Comment
String snake = "Rattlesnake"; "Rattlesnake" Create original String
snake.substring(0,6) "Rattle" Characters starting at character 0 to character 6-1
snake.substring(0,11) "Rattlesnake" 0 to length includes all the characters of the original string
snake.substring(10, 11) "e" Character 10 is the last one
snake.substring(6,6) "" If beginIndex==endIndex, an empty substring is created.
snake.substring(5,3)   If beginIndex is greater than endIndex, an exception is thrown.
snake.substring(5,12)   if endIndex is greater than length, an exception is thrown.

Control Characters inside String Objects

class BeautyShock
{
  public static void main (String[] arg)
  {
    String line1 = "Only to the wanderer comes\n";
    String line2 = "Ever new this shock of beauty\n";

    String poem  = line1 + line2;

    System.out.print( poem );
  }
}    

Recall that control characters are bit patterns that indicate such things as the end of a line or page separations. Other control characters represent the mechanical activities of old communications equipment. The characters that a String object contains can include control characters. Examine the program.

The sequence \n represents the control character that ends a line. This control character is embedded in the data of the strings. The object referenced by poem has two of them. The program writes to the monitor:

Only to the wanderer comes
Ever new this shock of beauty

Although you do not see them in the output, the control characters are part of the data in the String.

Here is another method from the String class:

public String toLowerCase();

This method constructs a new String object containing all lower case letters. There is also a method that produces a new String object containing all upper case letters:

public String toUpperCase();

QUESTION 22:

Here are some lines of code. Which ones are correct?

Line of code: Correct or Not?
String line = "The Sky was like a WaterDrop" ;
String a = line.toLowerCase();
String b = toLowerCase( line );
String c = toLowerCase( "IN THE SHADOW OF A THORN");
String d = "Clear, Tranquil, Beautiful".toLowerCase();
System.out.println( "Dark, forlorn...".toLowerCase() );

go to previous page   go to home page   go to next page