Learn Java the Hard Way (Second Edition)

Exercise 3: Printing Choices

Java has two common commands used to display things on the screen. So far we have only looked at println(), but print() is sometimes used, too. This exercise will demonstrate the difference.

Type the following code into a single file. By reading the code, could you guess that the file must be called PrintingChoices.java? In future assignments, I may not tell you what to name the Java file.

 1 public class PrintingChoices {
 2     public static void main( String[] args ) {
 3         System.out.println( "Alpha" );
 4         System.out.println( "Bravo" );
 6         System.out.println( "Charlie" );
 7         System.out.println( "Delta" );
 8         System.out.println();
10         System.out.print( "Echo" );
11         System.out.print( "Foxtrot" );
13         System.out.println( "Golf" );
14         System.out.print( "Hotel" );
15         System.out.println();
16         System.out.println( "India" );
18         System.out.println();
19         System.out.println( "This" + " " + "is" + " " + "a" + " test." );
20     }
21 }

When you run it, this is what you should see.

Can you figure out the difference?

Both print() and println() display on the screen whatever is between the quotation marks. But println() moves to a new line after finishing printing, and print() does not: it displays and then leaves the cursor right at the end of the line so that the following printing statement picks up from that same position on the line.

You will also notice (on line 8) that we can have a println() statement with nothing between the parentheses. No quotation marks or anything. That statement instructs the computer to print nothing, and then move the cursor to the beginning of the next line.

You may also notice that this program has some lines with nothing on them (lines 5, 9, 12, and 17). On the very first exercise, when I wrote that you must “match what I have written exactly, including spacing, punctuation, and capitalization”, I wasn’t quite being honest. Extra blank lines in your code are ignored by the Java compiler. You can put them in or remove them, and the program will work exactly the same.

My students often accuse me of being “full of lies.” This is true. I have learned the hard way that when students are just learning something as difficult as programming, telling them the truth will confuse them too much. So I often over-simplify what I say, even when that makes it technically inaccurate.

If you already know how to program, and my “lies” offend you, then this book will be difficult to read. But for those that are just learning, I assure you that you want me to simplify things at first. I promise I’ll reveal the truth eventually.

Anyway, on line 19, I did one more new thing. So far you have only been printing a single thing inside quotation marks. But it is perfectly fine to print more than one thing, as long as you combine those things before printing.

So on line 19, I have six Strings1 in quotation marks: the word “this”, a space, the word “is”, a space, the word “a”, and finally a space followed by “test” followed by a period. There is a plus sign (“+”) between each of those six Strings, so there are a total of five plus signs on line 19. When you put a plus sign between Strings, Java adds2 them together to make one long thing-in-quotation-marks, and then displays that all at once.

If you have an error in your code, it is probably on line 19. Remembering to start and stop all the quotes correctly and getting all those details right is tricky.

Today’s lesson was hopefully relatively easy. Don’t worry, I’ll make up for it on the next one.



“Learn Java the Hard Way” is ©2013–2016 Graham Mitchell