Learn Java the Hard Way (Second Edition)

Exercise 1: An Important Message

In this exercise you will write a working program in Java to display an important message on the screen.

If you are not used to typing detailed instructions for a computer then this could be one of the harder exercises in the book. Computers are very stupid and if you don’t get every detail right, the computer won’t understand your instructions. But if you can get this exercise done and working, then there is a good chance that you will be able to handle every exercise in the book as long as you work on it every day and don’t quit.

Open the text editor you installed in Exercise 0 and type the following text into a single file named FirstProg.java. Make sure to match what I have written exactly, including spacing, punctuation, and capitalization.

1 public class FirstProg {
2     public static void main( String[] args ) {
3         System.out.println( "I am determined to learn how to code." );
4         System.out.println( "Today's date is" );
5     }
6 }

I have put line numbers in front of each line, but do not type the line numbers. They are only there so I can talk about the lines. Also, depending on whether or not you have saved the file yet, the different words may not be colored at all. Or if they are colored, they might be different colors than mine. These differences are fine.

I’m going to walk through this line-by-line, just to make sure you typed everything correctly.

The first line starts with the word public followed by a single space then the word class, a single space, the word FirstProg, a space, and then a character called a “brace”. You type a brace by holding down SHIFT and then pressing the ‘[’ key, which is usually to the right of the letter ‘P’.

The ‘F’ in “First” is capitalized, the ‘P’ in “Prog” is capitalized. There are only two capital letters in the first line. There are only three spaces.

Before I go on to the second line of the program, I should tell you what programmers usually call each funny symbol that appears in this program.

Okay, so back to the line-by-line. You have already typed the first line correctly.

You should start the second line by pressing TAB one time. Your cursor will move over several spaces (probably 4 or 8). Then type the word public again, one space, the word static, one space, the word void, one space, the word main followed by an open paren (no space between the word “main” and the paren). After the paren there’s one space, the word String with a capital ‘S’, an open and close square bracket right next to each other, one space, the word args, one space, a close parenthesis, one last space, and a second open curly brace.

So line two starts with a tab, has a total of seven spaces, and only the ‘S’ in “String” is capitalized. Whew.

The third line should start with two tabs. Your text editor may have already started your cursor directly underneath the ‘p’ in “public”. If so, you only have to press TAB once. If not, press it twice to get two tabs.

After the tabs, type the word System with a capital ‘S’, then a dot (period), then the word out, another dot, the word println (pronounced “PrintLine” even though there’s no ‘i’ or ‘e’ at the end), an open paren, a space, a quotation mark (open quote), the sentence I am determined to learn how to code. (the sentence ends with a period), then a close quote, a space, a close paren and a semicolon.

So line 3 has two tabs, nine spaces, two dots (and a period), an open and close quote, an open and close paren, and only two capital letters.

Line 4 is nearly identical to line 3 except that the sentence says Today's date is instead of the determination sentence.

Line 5 starts with only one tab. If your text editor put two tabs in there for you, you should be able to get rid of the extra tab by pressing BACKSPACE one time. Then after the tab there’s a close curly brace. The close curly brace should line up with the ‘p’ in public static, since that’s the line where the matching open brace is.

Finally, line 6 has no tabs and one more closing curly brace. You can press ENTER after line 6 or not: Java doesn’t care.

Notice that we have two open curly braces and two close curly braces in the file. Three open parens and three close parens. Two “open quotes” and two “close quotes”. One open square bracket and one close square bracket. They will always pair up like this.

Also notice that every time we did an open curly brace, the line(s) below it had more tabs at the beginning, and the lines below closing curly braces had fewer tabs.

Okay, now save this (if you haven’t already) as FirstProg.java and save it in the “javahard” folder you created in Exercise 0.

Make sure the file name matches mine exactly: the ‘F’ in “First” is capitalized, the ‘P’ in “Prog” is capitalized, and everything else is lowercase. And there should be no spaces in the file name. Java will refuse to run any program with a space in the file name. Also make sure the filename ends in .java and not .txt.

Compiling Your First Program

Now that the program has been written and hopefully contains no mistakes (we’ll see soon enough), launch your Terminal (or PowerShell) and change into the directory where the code is saved.

Then do a directory listing to make sure the Java file is there. On my computer, those commands look like this:

mitchell@localhost:~$ cd Documents
mitchell@localhost:~/Documents$ cd javahard
mitchell@localhost:~/Documents/javahard$ ls
FirstProg.java test.txt

In the future, since your terminal probably doesn’t look like mine, I am going to abbreviate the prompt like this:


That way it will be less confusing, since there is less “wrong” stuff to ignore and you only have to look at what you should type and what you should see.

Okay. We have typed a list of instructions in a programming language called Java. But the computer cannot execute our commands directly in this form. We have to give this file to a “compiler”, which is a program that will translate our instructions into something more like ones and zeros that the computer can execute. In Java that ones-and-zeros file is called “bytecode”. So we are going to run the Java compiler program to “compile” our Java source code into a bytecode file that we will be able to execute.

The Java compiler is named javac (the ‘c’ is for “compiler”). It is properly pronounced “java-see”, though many reasonable people say “jav-ack”. (Those people are incorrect, but they are reasonable.)

Anyway, we use javac to compile our program like so:


If you have extraordinary attention to detail and did everything that I told you, this command will take a second to run, and then the terminal will just display the prompt again without showing anything else.

However, if you made some sort of mistake, you will see an error like this:

Don’t worry too much about the particular error message. When it gets confused, the compiler tries to guess about what you might have done wrong. Unfortunately, the guesses are designed for expert programmers, so it usually doesn’t guess well for beginner-type mistakes.

Here is an example of a different error message you might get:

In this case, the compiler is actually right: the error is on line 3 and the specific error is that a semicolon was expected (';' expected). (The line ends with a colon (:) but it ought to be a semicolon (;).

Here’s one more:

This time it is a capitalization error. The code says public class Firstprog (note the lowercase ‘p’) but the filename is FirstProg.java. Because they don’t match exactly – capitalization and all – the compiler gets confused and bails out.

So if you have any error messages, fix them, then save your code, go back to the terminal and compile again.

Eventually you should get it right and it will compile with no errors and no message of any kind. Do a directory listing and you should see the bytecode file has appeared in the folder:

`FirstProg.class  FirstProg.java  test.txt`

Now that we have successfully created a bytecode file we can run it (or “execute” it) by running it through the Java Virtual Machine (JVM) program called java:


What You Should See

Are you stoked? You just wrote your first Java program and ran it! If you made it this far, then you almost certainly have what it takes to finish the book as long as you work on it every day and don’t quit.

What You Should See After Completing the Study Drills


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