Printing in Java

Introduction

One of the problems we had when we created our generic list type in pascal was how to print the list. Since we didn't know what type of object was going to be in the list we had to import our print function from the file containing the data type in the list. In some languages like c++ it is possible to override the print routine for each type of object. Java uses a slightly different ploy. First all classes are descendents of the class Object. One of the functions in object is toString. This function always returns a string. If you call on System.out.print(c) where c is a user defined type, Java searches for the implementation of toString and uses it to create the string printed in the window.

To see an example of this click here .