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 program above is really an example of a Java class which the programmer happened to name HelloWorld. The first letter in the name of a class should always be upper-case. Inside of the class there is one method named main. The main method is required in this example and must be spelled correctly.
- The string "hello world" is displayed on the console output screen when this program is executed. A string is a set of characters that are enclosed in quotation marks.
- The code is typed into a Java source code file which must be named exactly the same as the name of the class that it contains. Normally, we only put one class in each separate file. Therefore, 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.)
- System.out.println is the command that causes the program to display "hello world". For now, you should simply memorize this command but technically println is a method that creates the output. The System class is built into Java and out is an object that is found in the System class.
Objective #2: Use the println method.
- The println method is used to print numbers and words to the console output window.
- To print a phrase such as "hello world", you simply enclose the phrase in double quotes inside the parentheses after the println method as in
System.out.println("hello world");
- To print numbers, you can enclose them in double quotes or not as in
System.out.println(3.14);
or
System.out.println("3.14");
- You can also concatenate text with the concatenation operator ( + ) as in
System.out.println("hello" + " " + "world"); // displays "hello world"
- You can use the addition operator ( + ) to add two numbers as in
System.out.println(3 + 4); // displays 7
But note that if you enclose a number in double quotes it is treated as a string and the plus symbol acts as a concatenation operator as in
System.out.println("3" + "4"); // displays "34"
- On your own, try the statement
System.out.println("3" + 4);
to see if the + acts as an addition operator or a concatenation operator!
Objective #3: Understand the difference between the print and println methods.
- The print method
does not cause a new line to be printed after its string argument.
- The println method does cause a new line to be printed after its string argument as if someone pressed the Enter key. You can think of the println method as "print line".
- Study the following examples to understand the difference between the print and println methods.
example |
displayed output |
System.out.println("Alice");
System.out.println("Bob");
System.out.println("Cathy"); |
Alice
Bob
Cathy |
System.out.print("Alice");
System.out.print("Bob");
System.out.print("Cathy"); |
AliceBobCathy |
System.out.println("Alice");
System.out.print("Bob");
System.out.print("Cathy"); |
Alice
BobCathy |
System.out.print("Alice");
System.out.println();
System.out.print("Bob");
System.out.println();
System.out.print("Cathy"); |
Alice
Bob
Cathy |
System.out.println("Alice");
System.out.println();
System.out.println("Bob");
System.out.println();
System.out.println("Cathy"); |
Alice
Bob
Cathy |
System.out.print("Alice");
System.out.print(" ");
System.out.print("Bob");
System.out.print(" ");
System.out.print("Cathy"); |
Alice Bob Cathy |
Objective #4: Understand how to use the escape sequences \", \n, and \\.
- 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.
Escape sequences that you must know for the AP exam are:
\"
\n
\\
- Because
double quotes are used to surround (i.e. delimit) strings, how would you force double quotes to print out? If you tried this line of code:
System.out.println("He said, "Come over here!"");
a compile error would occur since the double quote before the uppercase 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 inner set of 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 inner 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.
When you precede a double quote symbol with a backslash,
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.
Another example of using \n is illustrated with this example.
System.out.println("Sunday\nMonday\nTuesday\nWednesday\nThursday\nFriday\nSaturday");
would cause the days of the week to display on separate lines of output as in the following output:
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
The statement
System.out.println("Sunday\n\n\nMonday");
would cause the following to display:
Sunday
Monday
where the three \n escape sequences cause two blank lines to appear between Sunday and Monday. The first \n brings the cursor to the next line and the other two \n escape sequences cause the two blank lines to appear.
- 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.
- There are several other useful escape sequences.
However, only the three escape sequences \", \n, and \\ are tested on the
AP exam.