Java Reference
In-Depth Information
Display 1.1
A Sample Java Program
Name of class
(program)
1
public
class
FirstProgram
2{
3
public static void
main(String[] args)
4 {
5 System.out.println("Hello reader.");
6 System.out.println("Welcome to Java.");
The
main
method
7 System.out.println("Let's demonstrate a simple calculation.");
8
int
answer;
9 answer = 2 + 2;
10 System.out.println("2 plus 2 is " + answer);
11 }
12 }
Sample Dialogue
Hello reader.
Welcome to Java.
Let's demonstrate a simple calculation.
2 plus 2 is 4
The next two lines, shown in what follows, are the first actions the program performs:
println
System.out.println("Hello reader.");
System.out.println("Welcome to Java.");
. Each one causes the quoted
string given within the parentheses to be output to the screen. For example, consider
Each of these lines begins with
System.out.println
System.out.println("Hello reader.");
This causes the line
Hello reader.
to be written to the screen. The output produced by the next line that begins with
will go on the following line. Thus, these two lines cause the fol-
System.out.println
lowing output:
Hello reader.
Welcome to Java.
are a way of saying “output what
is shown in parentheses,” and the details of why the instruction is written this way
need not concern us yet. However, we can tell you a little about what is going on here.
These lines that begin with
System.out.
println
System.out.println