Declaring and Assigning


In a sense, computer programming is the art of assigning the right value to the right data at the right time. In Java, as in many other languages, you have to declare your data before you use it. Declaring means telling the compiler the types of data you will be using. In this section you will see how to declare and assign data, and will look at your first complete Java program.

When you programmed SimCom, you had to specify the address of each data operand. That meant you had to remember what you were using the different memory bytes for. For example, the Times 5 program used byte #29 as a loop counter and byte #30 for storing the result. Yet, when you look at the program for the first time, it's very difficult to tell what's going on.

In Java, you never have to remember which memory location is being used for which purpose. In fact, there is no way to even know which memory location is being used for which purpose. You pick a name for each memory location you want to use, and you refer to memory locations by name rather by address. The compiler assigns the addresses. All you have to do is tell the compiler the names you will be using, and the data type associated with each name.

For example, if you wanted to use a byte as a loop counter, it would be reasonable to choose the name loopCounter. Then you would declare as follows:

byte loopCounter;

A piece of memory that is declared and named in this way is known as a variable, so we will use that term from here on.

A declaration has three parts: a data type, a name, and a semicolon.

The data type (for now) is one of the eight primitive types: byte, short, int, long, float, double, char, and boolean. Later we will introduce some other types.

The name has to begin with a letter, an underline (_), or a dollar sign ($). The rest of the name can consist of letters, underlines, dollar signs, or digits. It is good programming practice to use variable names that begin with lowercase letters. If the name consists of more than one word, the second word and all subsequent words begin with uppercase letters. This is what we have done with loopCounter. Later in this book, you will see that there are other entities besides variables for which you will assign names (including classes and interfaces). These entities use different naming conventions. Following the conventions helps make source code easy to read.

The semicolon is a vital part of a declaration. A declaration is a kind of statement. A statement is a single instruction. All statements must end with a semicolon. Otherwise, the compilation will fail and the compiler will print out an error message with the line number where it ran into trouble.

Be aware that it is inherently impossible to create a compiler that produces consistently helpful error messages. Imagine someone running along a rough cobblestone road. If his foot slips on a stone, he might stagger for a few steps before falling. Similarly, if the compiler slips on an ungrammatical line, it might stagger over a few more lines before crashing and printing a message.

For the sake of convenience, you can declare multiple variables in a single statement, as long as the variables are all of the same type. So the following:

double mass, velocity, energy;

is equivalent to the following:

double mass; double velocity; double energy;

After you declare a variable, you can assign values to it. The following two lines declare and assign a variable called velocity:

double velocity; velocity = 123.456;

Notice that the assignment statement, like the declaration statement, ends with a semicolon. An assignment statement has the form variable = value semicolon. (In the next chapter, you will see how the value can be a complicated mathematical formula. For now, the value will be a simple literal number.) Be aware that the equal sign is just a symbol, and its meaning is not exactly the same as its meaning in a mathematical context. In geometry, when we say Area = πr2, the equal sign means "is, always has been, and always will be." In Java, the

equal sign means "store the value to the right of the equal sign in the variable to the left of the equal sign."

When you assign to a char variable, the easiest approach is to enclose the value in single quotes, like this:

char ch; c = 'w';

After execution, the variable ch contains the Unicode representation for the letter w. The single quotes can also contain special codes, called escape codes, that encode special characters. The most useful of these are

  • '\n' – Newline

  • '\t' – Tab




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net