Next: Variables. Up: Introduction. Previous: A basic program.


Pieces of a program

Textbook: Section 2.3

A Java program is a combination of basically three types of data.

Code

The most important part of the program is the code. This is the part that is actually meaningful to the compiler.

Java ignores all space in a program - it only pays attentions to actual visible letters. When a Java compiler sees a program, the first thing it does is break it into a sequence of tokens. A token is an atomic piece of a program that is meaningful to the compiler. Our particular program breaks into the following atomic pieces. Here are the tokens of the above program.

import          csbsju          .               cs160
.               *               ;               public
class           Hello           {               public
static          void            run             (
)               {               IO              .
println         (               "hello  world!" )
;               }               }
A token is basically a word or a symbol. But in the case of the string of letters "hello world!", it kept the whole thing together, since in the structure of the program, the only important thing is that these characters constitute a string.

White space

Tabs, spaces, and end-of-lines are completely immaterial part of the program as far as the compiler is concerned. But they're integral to the program from the programmer's point of view. For our eyes, the white space is critical to discerning structure in a program.

So it's important when you're programming to pay close attention to white space. You'll see that I use a particular system: Every time I reach a set of braces, I put an opening brace at the end of a line, indent everything within the brace, and put a matching brace on the previous level of indentation.

brace-block prefix {
    first line of brace-block body
    second line of brace-block body
    last line of brace-block body
}
This is a standard technique of white space placement that has stood the test of time for thirty years. There are a couple other techniques that people have developed that have also been in wide use for thirty years - those are fine too.

But it's critical that you choose a rigorous, well-tested white-space insertion technique and stick to it rigorously. Without consistent, good white space, programs are much more difficult to understand.

I also find it very useful to insert blank lines every 10 lines or so - wherever I'm starting a relatively new concept. I think of these as paragraphs. This is because long blocks of code are difficult to understand - you need something to break it up into conceptual pieces.

Comments

We haven't seen comments yet, but they constitute a third type of data occurring in a program. There are two types of Java comments.

Like whitespace, comments are also trimmed off by the compiler before it begins the real work. But comments are useful for documenting how a program works. Generally, they should be there to describe the forest instead of the trees - the code itself usually does the trees quite well, but it's difficult to discern the overall point of the program without some comments explaining.

You should generally use the first type of comment wherever possible. If you do this rigorously, then the second type becomes very useful whenever you want to try removing a large segment of the program at once. (You can't put the second type of comment within itself, since the */ of the inside comment will terminate the overall comment. So if you use the second type for normal programming, it's quite inconvenient to try to comment out large pieces of the program during debugging.)


Next: Variables. Up: Introduction. Previous: A basic program.