15-100 Lecture 3 (Friday, September 1, 2006)

A Quick Review: HelloWorld

Last class, we created and ran the following simple program:

  class HelloWorld {
    public static void main (String[] args) {
      System.out.println ("Hello World");

We talked very briefly about what the various parts of the program mean, but we mostly just observed that, at a very high level, it seemed that it might print "Hello World" to the screen -- and it did.

Today, we're going to go over the anatomy of this program one more time -- and then start modifying it a bit.

A Quick Read: System.out.println("Hello World");

Let's begin with this line:

      System.out.println ("Hello World");

Read it. Don't become distracted by the strange use of punctuation. What does it seem to be doing? "println". Print. Print Line. Okay. It sounds like it is printing the line, "Hello World". And, that's exactly what it is doing.

So, what is the "System.out" part? "System" refers to the computer. The "." is what we call the "scope" operator. It identifies the "out" as being within or associated with the "System". In other words, "System.out" refers to the "out" within the "System". What is "out"? It is the "output sub system".

So, taken together, we are asking the output subsystem of our computer to print the line, "Hello World".

The Argument List: System.out.println("Hello World");

Now, what about the ()-parenthesis? Why are they there. It wouldn't make sense to tell the output subsystem to print, without telling it what to print, right? It needs to know what to print.

In class, we went through some "human examples". I walked up to a student and asked, "Add 4 and 5". The student responded "9". Then, I walked to another student and asked, "Add (.)". The student inquired, "Add what?". Next, I asked one student to stand up. Then to sit down. No questions that time.

The point of the "human examples" was to observe that sometimes, when we ask for things, we need to supply more information -- and, sometimes, we don't.

The sytax of the Java language provides for an "argument list", or a comma-separated list of items that accompany a request. This list is enclosed within the parenthesis. In the case of println(), it contains what we'd like printed. In other cases, it can contain more than one thing. Sometimes, it can be empty -- but, even then, the parenthesis must still be there.

We'll talk more about the argument list later. For now, its just important to know that we tell println() what to print within the ()-parenthesis.

"What's with the quotes?"

Let's look again at our simple program:

  class HelloWorld {
    public static void main (String[] args) {
      System.out.println ("Hello World");

What is "Hello World" quoted? The answer is that the ""-quotes help the computer as it is reading the program. What is contained within the quotes is known as a "String literal". In other words, it is a collection of letters that use, without trying to understand.

The quotes serve the same purpose within the program as they would within English writing. When we use someone else's language in our own writing, we surround it with quotes. We do this to clearly distinguish where the other person's writing begins and ends.

The Soon To Be Ubiquitous ;-Semicolon

Notice the ;-semicolon at then end of the line. The ;-semicolon in Java is very similar to the .-period in English. It is used to distinguish the end of a "statement", a language construct similar to a sentence.

Although, for clarity, programmers usually write only one "statement" per line, it is possible to write more than one statement per line, just as more than one English sentence can be written on the same line. In these cases, the ;-semicolon helps Java distinguish the lines in order to understand the program. But, they are always required, even if there is only one statement per line.

Program Structure: What are the {}-braces?

When describing large things, people generally try to simplify the process by decompsing the problem into smaller, but still meaningful, parts. When programming in Java, we decompose problems into the description of "classes" of "objects" and into the "methods" by which they behave.

The funny language will become more meaningful to you over time. But, for now the idea is that we try to take small things and describe them, and then build them up into larger systems. When descrbing things, one of the key aspects is what they do.

The "class specification", is the basic buidling block. It is a document that describes everything about some type of thing. In this case, you see the overall structure:

  class HelloWorld {



"class HelloWorld" tells Java that we are about to describe a new type of thing, a "HelloWorld" thing. The description begins with the {-brace and ends with the }-brace. In other words, when you see the {-brace, understand, "This is the beginning". And, when you see the }-brace, understand, "This is the ending." Between the beginning and the ending lies the "body", itself. The body is where the class is actually decribed.

Within the body of a class specification, we need to describe that type of thing. Very often, this will involve describing one or more behaviors. In the case of our "Hello World" class, we describe one behavior, printing out, "Hello World".

We describe each behavior by giving it a name, explaining what arguments it needs, what it gives back, and how it works. This is called a "method specification". Think, "Method behind the madness". A description of how the behavior will come about.

Each class has a special, distinguished, behavior, the "main method". This method is special because it is the one started automatically if Java is asked to start a particular class. In other words, when we type "java HelloWorld", it looks for the main() method within HelloWorld.class and start it.

Although the body of the main() method is something that we can control, its "signature" is not. In other words, we cannot change its name, argument list, or return type.

We'll talk more about methods later. For right now, just remember the form of the main() method and know that we'll be coding within it.

We're Here To Help!
As always, we're here to help -- please let us know how we can be of service.