Learn Programming in Java


<<Previous | ToC | Next >>
 

Lesson #1: Sequence & Output

You already know something about output, your first program output some text to the output window. You can change what it puts there. Now to understand what is going on, let's look at the rest of the line:
System.out.println("Hello World!");


Notice I did not repeat the right half of the line, "/// REPLACE THIS LINE" because that is a comment. Comments are there for people -- in this case, you -- to look at, and the Java compiler ignores them. Everything we do for the next two or three days will essentially replace this one line only. Later we will learn about the other stuff in this window. There are two kinds of comments, but mostly we will use this kind, which starts with a double slant "//" and ends at the end of the line. This one has three slants "///" but the third one is part of the comment proper and has no other significance.

(Almost) everything that ends in a semicolon is called a statement, sort of like a sentence in English. It tells the computer to do one thing, but sometimes that one thing can be quite complicated. The dots are not sentence markers, they are used in Java (and other languages like C) to connect separate names together into a whole name. "System" is like the family name, and "println" is the individual name. System is a big family, so we are asking for the println that belongs to "out" rather than some other member of the System family (called a "class" in Java). That will make more sense when we start looking into classes. For now you are asking for a known System service called println. It prints one text item on a line of the output window, whatever is between the parentheses -- in quotes here, but in the next lesson we will see other kinds of things that can be printed.

There are other kinds of output, like saving text or other data to a file, or creating and showing pictures, or making sounds, but we'll ignore all those for now. Fundamentally, you do those things the same way, except you call a different service to do it.

For now we'll focus on the order things happen. Everything we do for the next two or three days will be to this one line in the white space inside the yellow rectangle in this window, usually by adding other lines immediately before or after it (still inside the white space, which will grow to accommodate what we added), and then seeing how the output is different.

So let's copy the one line and paste it in as a second copy and then change the text they output so the two lines are different, like this:

System.out.println("Hello Once");
System.out.println("Hello Twice");
Compile and run it again.

Bad Things Happen, hopefully not to you today, this minute, but they might. Usually it's because you made a typing error, or didn't think clearly about what you wanted to do. We all make mistakes. The nice thing about programming is that the computer doesn't care about your mistakes. It can't go to the next step, but that's your problem, not the computer's. Nothing breaks, just fix the problem and resume. It can wait all day for you. Or not, your choice.

Some errors the computer knows about, and can tell you. Java is better than most programming languages. In BlueJ, the errors the computer knows about will be marked with some mark (like a red underline) on the word or symbol that the compiler decided was the error, plus a big dark red bar in the margin that is easy to see. Hover your mouse over the marked symbol, and a message will pop up telling you what it thinks the mistake is. Often it's wrong -- especially on the second or third error -- about what exactly you did wrong, but at least you know where to look. For these early efforts you will always have my example code here in the web page to look at and compare to. Capital letters matter, spaces mostly don't (unless it breaks one word into two). Semicolons are not optional. Stuff like that.

After you fixed the error and the red marks go away, you can click on the compile button and resume -- but it might find more errors for you to fix. I noticed that sometimes BlueJ did not respond to the Compile button on the program window, but it did work to click the Compile button on the control window (where the document icons are). The yellow rectangle there will have gray diagonal lines across it if it needs compiling, and red cross-hatch if there are errors that need correcting (a later version of BlueJ may mark things differently, programmers like to change things to keep people confused, but I hope you do not become that kind of programmer: we need to help people, not befuddle them).

If all else fails, Quit/Exit out of BlueJ and restart it (usually it will remember where your windows are, but apparently not in ChromeBook). It's their fault, not yours. Or rather the fault of Unix, which is what they teach programmers in school. You're a smart cookie, you can learn to get around their mistakes. Don't let anybody tell you otherwise.

Anyway, after you successfully compile and run your modified program, notice that the first line outputs its text first, then on a separate line the second statement does its thing. In general, the lines run in the order they occur in the program. There are important exceptions, which we will get to shortly. Some programming languages (like Lisp) do not have a way to string statements together in sequence, so to get things to happen in a particular order requires some tricks that are unnecessary in Java. Not your problem (today).

Change your program to print out the words from your favorite song. You can do that, right? You just put in another println for each line of the song. That could get boring, but I wanted you to have some practice modifying and recompiling your program.

While we are still on the subject of statements, I should mention two other kinds of statements you will run into -- and sometimes need to use: the empty statement which is just a lone semicolon, and a sequence of statements grouped together by surrounding braces and thus considered as one statement. There could be any number of statements (including none at all) inside the braces, possibly including nested braces. Each line in the following program fragment is a single statement, except the last line is four statements:

System.out.println("Hello World!");
{System.out.println("one"); System.out.println("two"); System.out.println("ten");}
{}
{{{};{}}{}}
;;;;
The second line has three statements inside the brace-group. The next-last line is a single brace-group that contains two statements, both brace-groups. The second one is empty (no statements inside) but the first one contains three statements: an empty brace pair, followed by a null statement (the single semicolon), followed by another empty brace pair. You see how that works?

Next: Variables & Expressions

<<Previous | ToC | Next >>

Revised: 2021 May 8