Prints

From GCHSWiki
Jump to: navigation, search

In the following line of code we invoked a method through an object as follows:

System.out.println (“Hello Gulf Coast High School.”);

The System.out object represents an output device or file, which by default is the monitor screen. To be more precise, the object’s name is out and it is stored in the System class. We explore that relationship in more detail at the appropriate point in the text.

The println method represents a service that the System.out object performs for us. Whenever we request it, the object will print a string of characters to the screen. We can say that we send the println message to the System.out object to request that some text be printed. Each piece of data that we send to a method is called a parameter. In this case, the println method takes only one parameter: the string of characters to be printed.

The System.out object also provides another service we can use: the print method. Let’s look at both of these services in more detail.

The Print and Println Methods

The difference between print and println is small but important. The println method prints the information sent to it, then moves to the beginning of the next line. The print method is similar to println, but does not advance to the next line when completed.

Carefully compare the output of the Countdown program to the program code. Note that the word Liftoff is printed on the same line as the first few words, even though it is printed using the println method. Remember that the println method moves to the beginning of the next line after the information passed to it is printed. Often it is helpful to use graphics to show objects and their interaction. The figure below shows part of the situation that occurs in the Countdown program. The Countdown class, with its main method, is shown invoking the println method of the System.out object.

//********************************************************************
//
// Demonstrates the difference between print and println.
//********************************************************************
public class Countdown{
//-----------------------------------------------------------------
// Prints two lines of output representing a rocket countdown.
//-----------------------------------------------------------------
   public static void main (String[] args){
      System.out.print ("Three... ");
      System.out.print ("Two... ");
      System.out.print ("One... ");
      System.out.print ("Zero... ");
      System.out.println ("Liftoff!"); // appears on first output line
      System.out.println ("Houston, we have a problem.");
  }
}
 

Three . . . Two . . . One . . . Zero . . . Liftoff!
Houston, we have a problem.

String Literals

A character string is an object in Java, defined by the class String. Because strings are so fundamental to computer programming, Java provides the ability to use a string literal, delimited by double quotation characters, as we’ve seen in previous examples. We explore the String class and its methods in more detail later in this chapter. For now, let’s explore two other useful details about strings: concatenation and escape sequences.

Concatenation

The program below shows several println statements. The first one prints a sentence that is somewhat long and will not fit on one line of the program. A character string, delimited by the double quotation character, cannot be split between two lines of code. One way to get around this problem is to use the string concatenation operator, the plus sign (+). String concatenation produces one string in which the second string is appended to the first. The string concatenation operation in the first println statement results in one large string that is passed to the method and printed.

//********************************************************************
//
// Demonstrates the use of the string concatenation operator and the
// automatic conversion of an integer to a string.
//********************************************************************
public class Facts {
   //-----------------------------------------------------------------
   // Prints various facts.
   //-----------------------------------------------------------------
   public static void main (String[] args) {
   // Strings can be concatenated into one long string
      System.out.println ("We present the following facts for your "
      + "extracurricular edification:");
      System.out.println ();
      // A string can contain numeric digits
      System.out.println ("Letters in the Hawaiian alphabet: 12");
      // A numeric value can be concatenated to a string
      System.out.println ("Dialing code for Antarctica: " + 672);
      System.out.println ("Year in which Leonardo da Vinci invented "
      + "the parachute: " + 1515);
      System.out.println ("Speed of ketchup: " + 40 + " km per year");
   }

}


We present the following facts for your extracurricular edification:
Letters in the Hawaiian alphabet: 12
Dialing code for Antarctica: 672
Year in which Leonardo da Vinci invented the parachute: 1515
Speed of ketchup: 40 km per year


Escape Sequences

Because the double quotation character (“) is used in the Java language to indicate the beginning and end of a string, we must use a special technique to print the quotation character. If we simply put it in a string (“””), the compiler gets confused because it thinks the second quotation character is the end of the string and doesn’t know what to do with the third one. This results in a compile-time error.

To overcome this problem, Java defines several escape sequences to represent special characters. An escape sequence begins with the backslash character (\), and indicates that the character or characters that follow should be interpreted in a special way.

The program below, called Roses, prints some text resembling a poem. It uses only one println statement to do so, despite the fact that the poem is several lines long. Note the escape sequences used throughout the string. The \n escape sequence forces the output to a new line, and the \t escape sequence represents a tab character. The \” escape sequence ensures that the quote character is treated as part of the string, not the termination of it, which enables it to be printed as part of the output.

//********************************************************************
//
// Demonstrates the difference between the addition and string
// concatenation operators.
//********************************************************************
public class Addition
{
//-----------------------------------------------------------------
// Concatenates and adds two numbers and prints the results.
//-----------------------------------------------------------------
   public static void main (String[] args){
      System.out.println ("24 and 45 concatenated: " + 24 + 45);
      System.out.println ("24 and 45 added: " + (24 + 45));
   }
}


24 and 45 concatenated: 2445
24 and 45 added: 69

The Escape Sequences

The Escape Sequences
Escape Sequence Meaning
\b backspace
\t tab
\n newline
\r carriage return
\" double quote
\' single quote
\\ backslash

The newline, double quote and backslash are part of the Java subset and are testable on the AP test.