Richard G Baldwin (512) 223-4758, baldwin@austin.cc.tx.us, http://www2.austin.cc.tx.us/baldwin/

Using the System and PrintStream Classes

Java Programming, Lecture Notes # 16, Revised 10/03/99.

Preface
Introduction
Discussion
A Word about Class Variables
Review Questions for Lesson 16

Preface

Students in Prof. Baldwin's Introductory Java Programming classes at ACC are responsible for knowing and understanding all of the material in this lesson (except that they are not responsible for detailed information that is specific to C++).

The detailed material on C++ is provided as supplementary material for the benefit of those persons who are already familiar with C++ and who are making the transition into Java.

Introduction

This lesson introduces you to the use of the System and PrintStream classes in Java. This material is our first introduction to the complexity that can accompany the OOP paradigm. It gets a little complicated, so you might need to pay special attention to the discussion.

Discussion

What Does main() Do?

The main method in the controlling class of a Java application controls the flow of the program much as the main function in a C++ program controls the flow of a C++ program.

The main method can also access other classes along with the variables and methods of those classes and of objects instantiated from those classes.

The hello1 Application

Our hello1 class is repeated below for convenience.
 

/*File hello1.java Copyright 1997, R.G.Baldwin
**********************************************************/
class hello1 { //define the controlling class
  //define main method
  public static void main(String[] args){
    //display text string
    System.out.println("Hello World");
  }//end main
}//End hello1 class.  No semicolon at end of Java class.

Does this Program Instantiate Objects?

This is a simple example which does not instantiate objects of any other class.

Does this Program Access Another Class?

However, it does access another class which is the System class that is provided with the Java development kit. (The System class will be discussed in more detail in a subsequent lesson.)

The out Variable

The out variable referred to above as System.out is a class variable of the System class.

Remember, a class variable can be accessed without the requirement to instantiate an object of the class. Remember also that the class variable (as is the case with all variables) must be of some specific type.

Primitive Variables vs. Reference Variables

A class variable may be of a primitive type, or it may be a reference variable, which refers to an object.

(I'll have more to say about the difference between primitive and reference variables in a subsequent lesson.)

As you will see, the variable named out in this case is a reference variable, which refers to an object of another type.

Accessing Class Variables

You access class variables or class methods in Java by joining the name of the class to the name of the variable or method with a period.
 

In Java, System.out accesses the class variable named out in the Java class named System.

Scope Resolution Operator in C++ 

C++ programmers will recall that the scope resolution operator (::) is used to access class variables and class methods in C++.

The PrintStream Class

Another class that is provided with the Java development kit is the PrintStream class. The PrintStream class is in a package of classes that are used to provide stream I/O capability for Java.

What Does the out Variable Refer To?

The out variable in the System class refers to (points to) an instance of the PrintStream class (a PrintStream object) which is automatically instantiated when the System class is loaded into the application.
 

This is somewhat analogous to the fact that the input and output stream objects, cin and cout, are automatically instantiated and linked to the standard input and standard output devices when a C++ program begins execution. 

We will be discussing the PrintStream class along with a number of other classes in detail in a subsequent lesson on input/output streams, so this is not intended to be an exhaustive discussion.

The println() Method

The PrintStream class has an instance method named println() which causes its argument to be displayed on the standard output device.
 

Hopefully you already know about the standard input device and standard output device. If not, you need to go back and learn about them. 

In brief, The standard output device is normally the screen on the operator's console by default, but can be redirected to another device by the user at the operating system level.

Accessing an Instance Method

In the same way that instance variables are normally accessed in Java, the method named println() can be accessed by joining the name of a reference variable to an object of type PrintStream to the name of the method using a period.

Thus, assuming that the standard output device has not been redirected, the following statement causes the string Hello World to be displayed on the screen.
 

System.out.println("Hello World");

This statement invokes the println() method of an object of the PrintStream class which is referred to (pointed to) by the variable named out which is a class variable of the System class.

As I indicated when I started this lesson, this is our first introduction to the complexity that can result from use of the OOP paradigm. In gets worse in some cases. If this is not clear to you, you need to go back over it and think about it until it becomes clear.

A Word about Class Variables

How Many Instances of a Class Variable Exist?

The runtime system allocates a class variable only once no matter how many instances (objects) of the class are instantiated.

All objects of the class share the same physical memory space for the class variable.

Accessing a Class Variable

You can use the name of the class to access class variables by joining the name of the class to the name of the variable using a period.

You can also access a class variable by joining the name of an object of the class to the name of the variable using a period as the joining operator.

Referencing Object Methods via Class Variables

Class variables are themselves either primitive variables or are references to objects (instances of some class).

The referenced object may provide methods to control the behavior of the object. In the above case, we accessed the println() method of the single instance of an object of the PrintStream class referred to by the class variable named out.

Instance Variables and Methods

As a side note, in addition to class variables, both Java and C++ provide instance variables and instance methods. Every instance of a class has its own set of instance variables. You can only access instance variables and instance methods through an object of the class.

Review Questions for Lesson 16

Q- The main method in the controlling class of a Java application controls the flow of the program: True or False?

A - True

Q - The main method cannot access the variables and methods of objects instantiated from other classes: True or False?

A - False. The main method can access the variables and methods of objects instantiated from other classes. Otherwise, the flow of the program would be stuck within the main method itself and wouldn't be very useful.

Q - The main method must instantiate objects of other classes in order for the program to execute: True or False?

A - While it is probably true that the main method must instantiate objects of other classes in order to accomplish much that is of value, this is not a requirement. The main method in the "Hello World" program of this lesson does not instantiate objects of any class at all.

Q - In order to be useful, the System class must be used to instantiate objects in a Java application: True or False?

A - False. The System class has several class variables (including out and in) that are useful without the requirement to instantiate an object of the System class.

Q - Class variables such as the out variable of the System class must be of some specific type: True or False?

A - True

Q - Class variables must be of a primitive type such as int or float: True or False?

A - False. A class variable can be a primitive type, or it can be a reference variable which points to another object.

Q - The out variable in the System class is of a primitive type: True or False?

A - False. the variable out defined in the System class is a reference variable which points to an object of another type.

Q - What does the code fragment System.out access?

A - The code fragment System.out accesses the class variable named out in the class named System.

Q - An object of type PrintStream is automatically instantiated when the System class is loaded into an application: True or False?

A - True

Q - The out variable in the System class refers to an instance of what class?

A - The out variable in the System class refers to an instance of the PrintStream class (a PrintStream object) which is automatically instantiated when the System class is loaded into the application.

Q - The println method is an instance method of what class?

A - The println method is an instance method of the PrintStream class.

Q - What is the primary behavior of the println method?

A - The println method causes its argument to be displayed on the standard output device. (The standard output device is the screen by default, but can be redirected by the user at the operating system level.)

Q - How can the println method be accessed?

A - The println method can be accessed by joining the name of a variable that references a PrintStream object to the name of the println method using a period.

Q - Assuming that the standard output device has not been redirected, write a code fragment that will cause your name to be displayed on the screen.

A - System.out.println("Dick Baldwin");

Q - Explain how your code fragment produces the desired result.

A - The above statement invokes the println method of an object of the PrintStream class which is referenced (pointed to) by the out variable which is a class variable of the System class.

Q - If you have a class named MyClass that has a class variable named myClassVariable which requires four bytes of memory and you instantiate ten objects of type MyClass, how much total memory will be allocated to contain the allocated variables (assume that the class definition contains no other class, instance, or local variables).

A - The runtime system allocates a class variable only once no matter how many instances of the class are instantiated. Thus, all objects of the class share the same physical memory space for the class variable, and in this case, only four bytes of memory will be allocated to contain the allocated variables.

Q - In the code fragment

System.out.println("Dick Baldwin");

how many actual instances of the variable named out are allocated in memory?

A - Only one, because out is a class variable of the System class.

Q - If you have a class named MyClass that has an instance variable named myInstanceVariable which requires four bytes of memory and you instantiate ten objects of type MyClass, how much total memory will be allocated to contain the allocated variables (assume that the class definition contains no other class, instance, or local variables).

A - Every instance of a class has its own set of instance variables. You can only access instance variables and instance methods through an object of the class. In this case, forty bytes of memory would be required to contain the instance variables of the ten objects.

-end-