Chapter 3: Operations


The previous chapter showed you how to declare various types of primitive variables, and how to assign and print out the values of variables. This chapter will look at the computational operations that you can perform on Java data. For numeric data—that is, for all primitive types other than boolean—these computations include the familiar arithmetic operations of addition, subtraction, multiplication, and division, as well as some more exotic operations. Arithmetic operations are not applicable to boolean data, which has its own group of operations.

Before covering these topics, we will look at two ways to make programs easier to read: white space and comments.

White Space and Comments

This chapter is going to present some techniques for writing long and intricate programs. Before we begin, though, let's look at how to write programs that are easy to read and understand.

Consider the following lines of code:

int x; x = 5;

As far as the Java compiler is concerned, this code is identical to the following:

int x;         x=5;

These two versions produce exactly the same compiled bytecode. In the preceding line, the gap after the semicolon can be created by adding a few tabs or a lot of spaces. Either way, it doesn't matter to the compiler.

The compiler ignores any blank space created by using the spacebar or by typing Tab or Enter. Such space is called white space. You can use white space to make your source code more readable. For example, the following code declares four variables:

         double velocity; boolean b;               short x; long        hotSummer;

If you use this code in a program, someone who is unfamiliar with the program will have a hard time figuring out what your intention is. (And the person sweating over your source code could be yourself, reviewing your own code long after you originally wrote it.)

You can make your code more readable to humans by manipulating white space, like this:

double velocity; boolean b; short x; long hotSummer;

Or better yet:

double   velocity; boolean  b; short    x; long     hotSummer;

In the first example, some spaces and a return have been removed, and a return has been added, so that all the left edges line up. In the second example, white space has been added. Now the eye of any reader, including you, will subconsciously arrange the code into two columns. The words on the left are all data types, and the words on the right are all variable names. The columns are easily aligned: You just press Tab before each data type and before each variable name. This formatting scheme is so clear that it is considered correct style by convention. Any other formatting arrangement would be less readable, and would also be considered sloppy style.

The previous chapter presented the following simple program:

1. public class VerySimple 2. { 3.   public static void main(String[] args) 4.   { 5.     double age; 6.     age = 12.34; 7.   } 8. }
Note

Remember that the line numbers are not part of the source code. They are just included to make it easier to refer to particular lines.

A Java program consists of one or more class definitions (though we will not discuss class definitions until Chapter 7, "Introduction to Objects"). For now, be aware that lines 2-8 are the definition of a class named VerySimple. The class definition begins with an open curly bracket (line 2) and ends with a closed curly bracket (line 8). Since these brackets are vertically aligned in the same column, our brains notice that they are spatially related, and we subconsciously assume that they must also be functionally related.

A class definition can contain (among other things) a number of method definitions. Methods will be discussed in detail in Chapter 4, "Methods"; for now, you just need to be aware that lines 4-7 are the definition of something called a method, whose name is main. The method definition begins with an open curly bracket (line 4) and ends with a closed curly bracket (line 7). Again, the vertical alignment of the brackets gives us visual information about the structure of the program.

Notice how easy it is to look at lines 3 and 4, which tell us, "the method starts here," and find the end of the method. Within the method, all the code (lines 5 and 6) is vertically aligned. If you look at the listing with your eyes out of focus, all you see are several levels of nested blocks of blurry stuff. A Java program is (mostly) a block that contains blocks that contain blocks, etc. It is extremely important to use white space and indentation to indicate the nesting level of all your lines of code.

In addition to white space, the Java compiler also ignores comments. There are two kinds of comments: single-line and multi-line.

A single-line comment begins with two slashes (//). There can't be anything between the slashes. The compiler ignores everything from the slashes through the end of the line. This lets you put descriptive text after the slashes. Usually, the text explains what just happened in the line. For example:

float   distance;   // Units are microns double  weight;     // Units are ounces

Note the use of white space to vertically align the comments.

A multi-line comment, also known as a traditional comment, can span more than one line but doesn't have to. This kind of comment begins with a slash immediately followed by an asterisk (/*). The comment ends with an asterisk immediately followed by a slash (*/). For example:

/* Declare and initialize variables that will later be used for computing time-distortion effects at relativistic speeds. All distance units are miles, not kilometers. */ double    speedOfLight; int       numberOfPlanets; speedOfLight = 186000; numberOfPlanets = 9;

Note the use of blank lines to separate the multi-line comment from the declarations, and the declarations from the assignments.

Now that you know how to make source code easy to read, we can move on to the main topic of this chapter, which is how to write a program that makes your computer actually compute something.




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