Skip to main content
Lesson 3 - Data Types in Java
ZIPPDF (letter)
Lesson MenuPreviousNext
  
Printing Variables Using the System.out object page 6 of 11

  1. The System.out object is defined in each Java program. It has methods for displaying text strings and numbers in plain text format on the system display, which is sometimes referred to as the "console". For example:

    Program 3-2

    public class PrintVar
    {
      public static void main(String[] args)
      {
        int  number = 5;
        char  letter = 'E';
        double average = 3.95;
        boolean done = false;
    
        System.out.println("number = " + number);
        System.out.println("letter = " + letter);
        System.out.println("average = " + average);
        System.out.println("done = " + done);
        System.out.print("The ");
        System.out.println("End!");
      }
    }
    
    Run output:
    
    number = 5
    letter = E
    average = 3.95
    done = false
    The End!
    
  2. Method System.out.println displays (or prints) a line of text in the console window. When System.out.println completes its task, it automatically positions the output cursor (the location where the next character will be displayed) to the beginning of the next line in the console window (this is similar to pressing the Enter key when typing in a text editor - the cursor is repositioned at the beginning of the next line in your file).

  3. The expression

    "number = " + number

    from the statement

    System.out.println("number = " + number);

    uses the + operator to "add" a string (the literal "number = ") and number (the int variable containing the number 5). Java has a version of the + operator for String concatenation that enables a string and a value of another data type to be concatenated (added). The result of this operation is a new (and normally longer) string. String concatenation is discussed in more detail in the next lesson.

  4. The lines

    System.out.print("The ");
    System.out.println("End!");
    

    of Program 3-2 display one line in the console window. The first statement uses System.out's method, print, to display a string. Unlike println, print does not position the output cursor at the beginning of the next line in the console window after displaying its argument. The next character displayed in the console window appears immediately after the last character displayed with print.

  5. Note the distinction between sending a text constant, "number = ", versus a variable, number, to the System.out object. A boolean variable will be printed out as its representation of true or false.


Lesson MenuPreviousNext
Contact
 ©ICT 2003, All Rights Reserved.