Console Screen and System.out.println and Why?
By Lon Hosford
 
System.out.println is a method that displays on the operating system "console" screen.
 
The println method requires you to place a String expression inside the argument parenthesis. The String expression is then displayed on the console and the cursor on the console moves to the beginning of the next line.
 
Examples:
System.out.println("Hello World!");
 
String myFirstName = "Willy";
System.out.println("The first name is " + myFirstName);
String myLastName = "Nilly";
System.out.println("My name is " + myFirstName + " " + myLastName);
 
int totalHogsForSale;
int totalHogsOwned = 25;
int totalHogsSold = 10;
totalHogsForSale = totalHogsOwned - totalHogsSold;
System.out.println("Total hogs for sale: " + totalHogsForSale );
 
Hello World!
The first name is Willy
My name is Willy Nilly
Total hogs for sale: 15
The console screen is a character based screen where each character appears one after the next and the programmer needs to send the new line character to move to the next line.
 
As new lines are added the console scrolls upward and eventually out of view. This is unlike a GUI interface that can contain scrolling regions where you can scroll back.
 
So what good is System.out.println?
 
Program Line Tracer
It is handy for tracing what lines have occurred in a program.
// Bunch of program lines
System.out.println("I computing the total hours worked. Can you see me now?");
// Bunch more program lines that compute total hours worked
System.out.println("I am computing total day's pay. Can you see me now?");
// Bunch more program lines that compute total pay for the day's work
 
 
I computing the total hours worked. Can you see me now?
I am computing total day's pay. Can you see me now?
A possible use is if you are getting run time errors, you can see what lines processed before the error occurred.
 
Data Tracer
It is handy for tracing data in a program. You can observe the changes in data from line to line in a program. Here is an example that computes a store purchase with sales tax and a discount. The wrong answer keeps appear and so intermediate System.out.println lines are added to help find the problem.
 
float price    = 15;
int   quantity  = 3;
float salesTax  = 0.05;
float discount = 0.10;
float amountDue = price * quantity;
System.out.println("Is it correct now? Gross amount: "    + amountDue  );
amountDue = amountDue + (amountDue * discount) ;
System.out.println("Is it correct now? After discount: "  + amountDue  );
amountDue = amountDue - (amountDue * discount) ;
System.out.println("Is it correct now? After sales tax: " + amountDue  );
 
Is it correct now? Gross amount: 45.0
Is it correct now? After discount: 49.5
Is it correct now? After sales tax: 44.55
Hey what happened on the discount computation? Adding and not subtracting Ah Ha!
 
Testing Program
A good testing program is one that is the simplest that provides various inputs and outputs to code you are writing without any effort on your part. You simply run the program and compare the results to the expected results. We will use System.out.println to test classes we are building starting next week.
 
A Production Program
A simple program certainly can use the console. The System.out.println method may be the primary and cheapest method of output for simple results. GUI expensively consumes time and resources. Console interfaces are cheap and quick but not overly functional for a user interface. If data is a few simple responses from a user or comes say from a file, then a console program may be the quickest solution.