Java: Console Output

You can write programs that write text lines to the "console", which is typically a DOS command window. This is not as interesting as a Graphical User Interface, but you will see it used.

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
// introductory/ConsoleOutput.java
// Michael Maus, 25 August 2004
// This program shows a message on the console.

public class ConsoleOutput {
    
    public static void main(String[] args) {
        System.out.println("Hello, Earthling");
    }
}
No imports are required
The System class is automatically imported (as are all java.lang classes).
Line 8 - Write the output
You can write one complete output line to the console by calling the System.out.println() method. The argument to this method will be printed. println comes from Pascal and is short for "print line". There is also a similar print method which writes output to the console, but doesn't start a new line after the output.
Line 9 - No need to explicity stop the program
If there is no GUI, there's no need to explicitly stop the program. However, you can always add
   System.exit(0);
at the end if you wish.