Console Output
Objective #1: Compile and execute a simple Java program.
- Memorize the following "Hello World" Java program.
// John Doe
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("hello world");
}
}
- The name of the class is HelloWorld and can be chosen by the program. Except for the string "Hello World", the rest of this program consists of Java keywords and operators. A string is a set of characters that are enclosed in quotation marks.
- A Java source code file must be named to match the name of the class that it contains. The code above must be saved in a file named HelloWorld.java (using a 4-letter extension even if you're going to compile the program on a Windows computer.)
- Here is an interesting look at "hello world" programs in other computer languages.
- The System class is part of the API (application programming interface) provided with the Java environment. It gives you system-independent access to system-dependent functionality. In other words, it allows you to take advantage of graphic methods with a Java program even if you aren't sure of the platform that your Java program is going to be executed on.
- System.out is the full name of the out object in the System class. Notice that there is no instantiated System object. Rather, out is referred to directly from the class name. This is because out is a class variable rather than an instance variable. When you execute a program, one class variable is created per class, no matter how many instances exist of that class. So even if there is another System.out statement in the main function, the same out variable would be used.
- You can also associate methods with a class which are called class methods which should not be confused with instance methods.
- An escape sequence can be used inside
of a string to print special characters that would otherwise be impossible
to print due to the special role of certain characters. For example, because
double quotes are used to delimit strings, how would you print a double
quote if you actually wanted to? The statement
System.out.println("He said, "Come over here!"");
causes compiler errors since the double quote before the capital C is interpreted
as the end of the string. In order to print the desired quotation above,
you would need to use escape sequences to "escape" the inside double
quotes. The statement
System.out.println("He said, \"Come over here!\"");
would work correctly because it uses the escape sequence \" in
two places to indicate to the compiler that the inside double quotes should be
displayed
as double quotes. In Java (and many other computer languages), the backslash
is used as the escape operator. By preceding certain characters
with a backslash, you are indicating to the compiler to do something special.
In the case of the escape sequence \",
you are telling the compiler to display a double quote rather than to interpret
the double quote as the
beginning or end of a string literal.
Another escape sequence is \n which is used
to create a new line in console output.
System.out.print("hello world\n");
is more or less equivalent to
System.out.println("hello world");
since the \n causes
a new line after the phrase "hello world" prints just as the println
method causes a new line to print. Note that the print method
does NOT cause a new line to follow the phrase "hello world". The
statement
System.out.println("Sunday\nMonday\nTuesday\nWednesday\nThursday\nFriday\nSaturday");
would cause the days of the week to display on separate lines of output.
The escape sequence \\ causes a single backslash
to be displayed. The statement
System.out.println("some\\none");
would cause the string literal some\none to
display since the escape sequence \\ causes
a backslash to appear. Notice that the two consecutive characters \n are
not treated as an escape sequence in this example.
- See pp. 23+ for some other examples of useful escape sequences.
However, only the three escape sequences \", \n, and \\ are tested on the
AP exam.
- Long statements can be broken up into multiple lines of code. For example
int bigNum = 1 + 2 + 3 + 4 + 5 + 6 +
7 + 8 + 9 + 10;
will compile. You do not need to use a line continuation operator like you do in Visual Basic. But you cannot break up a string literal in this way
System.out.println("Four score and seven years ago
stuff happened");
However that statement could be written as
System.out.println("Four score and seven years ago " +
"stuff happened");
since the string literal was broken up into two string literals.
- There are three methods of creating comments in Java.
- You can begin a comment with // such as
// John Doe
- You can enclose a comment within the matching symbols /* .... */ such as
/*
Mr. Minich
Best Java Programmer in the world
*/
- You can enclose a javadoc comment within the symbols /**
.... */ as in
/**
The class Fish can be used to manipulate
a virtual fish.
*/
Notice that there are two asterisks in the opening comment tag. Javadoc
comments are used to create HTML help pages for the code that you
create. By enclosing javadoc comments along with your source code in
the appropriate places, a utility can be used that automatically creates
HTML Web pages that explain your code and serve as online help. In
fact, the online
Java API was built from javadoc comments.