Java Notes Prev: Java Example: Do Nothing | Next: Console: Take me to your leader

Java Example: Console: Hello Earthling

This is very similar to the first program, but it actually does something. The additional parts are described below.

Beginning textbooks often use console output. In this style text lines appear on the "console", which is often a DOS command window or a separate pane in the development system you're using. Console output doesn't work with normal Graphical User Interface (GUI) programs, so it's preferable to use dialog box output which is compatible with normal GUI programs.

// File   : introductory/
// Purpose: This program can be used by aliens for first contact.
// Author : Fred Swartz - 2007-03-26 - Placed in public domain.

public class Greeting {

    public static void main(String[] args) {
        System.out.println("Hello Earthling.");
        System.out.println("We come in peace.");

No imports are required
The System class is automatically imported (as are all java.lang classes).
Lines 8-9 - 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.

System.out.println is a predefined method. A method is a group of Java statements which are defined in one of the Java libraries. After a method name you enclose in parentheses all values you want to send to the method. These values are called arguments or parameters. Text messages must be enclosed in quotes. Statements are generally followed by a semicolon.

Dialog Output

See Java Example: Dialog: Hello Earthling for how this program would be written using dialog output.