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 "package" 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. 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");}
{}
{{{};{}}{}}
;;;;

Next: Variables & Expressions

<<Previous | ToC | Next >>

Revised: 2019 December 24