Copyright (c) 2000 by Charlie Calvert
In this chapter you will get a look at some of the basic syntactical elements used in a Java program. Topics covered include such fundamental elements as keywords, expressions and statements. I am covering these subjects so that I can use these terms meaningfully in the next few chapters, which cover subjects such as operators, branching and looping.
It is, of course, very important to have a sense of how the basics of a language fit together. If you have a solid base in a language, then you can always build on it when searching for ways to enhance your knowledge. However, if you don't have a solid base, then your attempts to enhance your knowledge can lead to the construction of rickety edifaces that are not reliable.
In order to talk intelligently about operators, branching and looping I'm going to have to use terms such as statement or expression. As a result, I'm going to make a stab at defining these hard to pin down terms. Then in the next chapter, I will be able to use them when talking about Java operators. In fact, an understanding of statements and expressions will be valuable in not just the next chapter, but in the next few chapters, and throughout your Java programming career. As it happens, the discussion of branching and looping will focus on particular kinds of statements, such as for, while, and if statements. All I'm doing in this chapter is laying the groundwork for the code you will see in those upcoming chapters.
Statements are the basic building blocks out of which programs are created. An application or applet begins by executing the first statement in your code, and they proceed on in a linear fashion executing each statement that you wrote, one at a time.
There are many types of commonly used statements in Java. They include while, if, for, switch, do, break, continue, throw, synchronized, and throw statements. A line of code in which one value is set equal to another is an assignment statement. Java has no goto statement, but it does have labels. When you are using a label you write a label statement. There is also something called an empty statement, and the declaration of a variable is also considered a statement. It is called, appropriately enough, a declaration statement. Finally, there is something called a block statement. A block statement begins with a curly brace and ends with a curly brace. Anything in between the two curly braces, including other statements, can be considered part of one large statement called a compound statement.
The previous paragraph contains a fairly complete list of all the statements available for you when you want to build a program in Java. As such, this list is probably a relatively precise definition of what a statement is, but I don't expect that it will mean much to most readers, since I haven't yet defined what all these statements are or why they exist. In the next section of the text, however, I will give a few examples of statements, and hopefully the ideas expressed in the last paragraph will start to make some sense to you after you study them for a bit. I will, of course, more formally define each of these elements of the language in later portions of this text.
Before I begin, I will try to give you a second, less precise way of thinking about statements. Please understand that it is difficult to come up with a general definition of the term statement that will still be meaningful to people who are relatively new to programming. When language lawyers really decide to pin this subject down, the resulting discussion can go on for pages, and much of it will be hard for any but the most advanced programmer to fully understand. Fortunately, I don't need that kind of precession in order to communicate the ideas that I want to express in this chapter or elsewhere in this book. As a result, I will talk in much more general terms.
Speaking very loosely, a statement is to a program much as a sentence is to a book. With the exception of compound statements, a statement is usually a relatively small syntactical element. It is, however, large enough to contain a complete thought or discrete logical entity.
Most statements end with a semicolon or they are bracketed by a pair of curly braces. A pair of curly braces denote a block of code. As explained in the section of this text called "compound statements," a block of code can itself contain multiple statements.
In a sense, when you add a semi-colon to a bit of code, or you set it off in curly braces, you are usually saying that you have come to the end of a thought. You have expressed a complete idea that can, given the addition of some variable declarations, stand on its own. As such, it is similar to, but by no means identical to, a sentence in prose. I should hasten to add, however, that not all syntactical elements in the Java language that stand on their own, and end with a semicolon, are statements. I am merely saying that a large percentage of the elements in your code that stand on their own and end with a semicolon happen to be statements.
It is time to move past a metaphorical description of statements and start seeing some of the details of how they actually work.
Here is a classic, and very simple, statement:
c = a + b; // assignment statement.
The example shown here is known as an assignment statement. As such, it is b built around the assignment operator, which in Java is an equals sign.
Note that there are no declarations in this statement. Somewhere else in the code you need to declare the type of the variables a, b and c. For instance, they might all be of type int. (The declaration for your local variables will itself be a statement.)
Nevertheless, despite the absence of the type declarations, the statement represents what I call a complete thought. That is, it's a discrete bit of code that stands on its own. From a grammatical point of view, what makes it a statement is the fact that it ends with a semicolon, but from a more semantic point of view, it does represent a relatively complete thought that the compiler can, given the presence of some type declarations, treat as a discrete entity.
Basic syntactical elements such as for, if and while statements will probably be familiar to most readers who have had experience with another computer language. I will show you a few examples of these kinds of statements. Once again, if they make no sense to you, don't worry, as I will describe these kinds of statements in more depth in a later chapter.
Consider the following statement:
if (x = 1)
i = 2;
The code shown here is an if statement. It is one complete statement, even though it appears on two lines.
Consider the following code:
while (x < 5) x++;
Again, this is one single statement, even though it appears on two lines. For now, it is not important that you understand what the statement means, or how it works, but only that you recognize it as a statement. The reason you know it is a statement is that it ends with a semicolon.
Consider this statement:
if (a > b)
b = 0;
else
a = 0;
This is an example of a single statement that consists of two parts, an if statement and an else clause. It happens that the code up to the first semicolon can stand on its own as a single statement. The code in the else clause cannot stand on its own, it makes sense only when taken as part of the larger statement. Again, it is not important that you understand what the statement means, only that you see that some statements work this way.
The reason the else clause is not a statement is that it will not stand on its own without the support of the main body of the if statement itself. Statements need to have a certain autonomy, or they are not really statements. It is, to revive the analogy used earlier, the same principle we find in sentences. A sentence represents a whole thought. In the same way, a statement represents a complete autonomous chunk of code, except for the missing declarations or possible references in the statement to other functions or objects.
NOTE: At this point you can begin to grasp why it is that statements are hard to define and hard to understand as anything other than a list of syntactical elements such as I presented at the beginning of this chapter. You could justifiably say at this point: "Hey Charlie, you just told us that statements end with a semicolon, yet now you are saying that here is an example of something that ends with a semicolon, that is not a statement." If you look back, you will see that my definition is actually qualified by the word "usually," but nonetheless, it is frustrating to find these exceptions to such a nice clean rule. It happens that the vast majority of the time my rule does hold, and I believe it gives you a place to start when trying to find the statements in your code.
Consider the following method:
int foo()
{
return 2;
}
The single line of code in this method, return 2, is a full statement. It is known, naturally enough, as a return statement. There are many other statements of this type, such as the break statement and the continue statement. (Again, my point here is not to try to define return statements or break statements, but only to show them to you as examples of the kind of code which is called a statement. In this case, you can recognize the piece of code as a statement in part from the presence of the semicolon and in part from simply memorizing the fact that the keyword return is part of a statement. (I would not necessarily bother trying to memorize facts like that, but instead simply be guided by the general rule about semicolons.)
Here is another type of statement called a compound statement:
{
int a, b, c;
c = a + b;
double b = a * c;
}
In this example there are several statements inside one set of curly braces. Each individual statement is a full statement, but all of them together are part of a compound statement, or block statement, as it is sometimes called. The block statement is defined by a set of matching curly braces.
Note that the declaration int a, b, c; is itself a statement. It is called a local variable declaration statement. The second statement in the compound statement is a simple assignment statement. The last statement is both an assignment statement and a declaration statement.
Language lawyers justifiable delight in odd cases, such as the empty statement, which consists of a single semicolor with nothing attached to it. I won't, however, try to pursue this subject any further, as we don't really need such a technical definition of statements in order to use the term in this book. All I'm really trying to do is give you a reasonably usable working definition of the term, and enough examples so that you will have some idea of what I'm talking about when I use the word "statement" in this text.
Once again, we can follow two very loosely defined rules: most statements end with a semicolon or else they are contained between two matching curly braces. That definition is not technical enough to pass muster with a language lawyer, but it should help you get a general feeling for the term. A more technical definition might simply end up listing all the possible kinds of statements, such as assignment statements, if statements, while statements, switch statements, empty statements, etc. Eventually, you might want to memorize such a list of statements, but you don't need that degree of rigor while you are still just getting a feeling for how the language works.
Expressions are a bit more abstract that statements, but ultimately I think they are easier to understand. This is largely due to the fact that it is easier to find rules that pin down exactly what an expression does.
Expressions yield results, that is, they return some kind of value. Most expressions involve at least one operator.
Speaking more generally, one can say that expressions are usually smaller than statements, and in fact they are often subsets of statements. It is not true, however, that all expressions need to be a subset of a statement. In short, there is something known as an expression statement. It is simultaneously both an expression and a statement.
Here are a few examples of expressions:
a + b; // Add a + b to yield a result doSomething(); // Method invocation
The first expression shown above will not stand on its own. Like all expressions, it yields a result. Langauge lawyers will sometimes call this denoting a variable or value. When we look at the statement c = a + b, the a + b part is known as an expression because it yields a result, which in this case is c. By itself, c is not an expression, since it yields no result. It is just a code fragment.
The second example is a method invocation: doSomething(). It might appear that this is not an expression, since it does not appear to yield a result. It is just a method call. Most methods, however, return a value. Even if the method doSomething is declared to be of type void, calling it is still considered an expression, since it yields the type void. (In Pascal, a procedure "yields the type void." In VB, a sub usually yields the type void. In C++ or Java, there is no such thing as a procedure or a sub, and instead they have functions or methods. All methods in Java either return a type such as an int, or else they return nothing, which means they return the type void.
Here is a function that returns int:
int foo()
{
return 2;
}
Here is a function that returns void:
void foo()
{
int a, b, c;
c = a + b;
}
A simple call to method of type void, or a method that returns a value, is called an expression statement. It is both an expression and a statement at the same time. The method itself is not an expression, it is only the call to the method that is an expression. It fits the definition of an expression because a call to a method yields a result -- the only difficulty here is simply understanding that the result returned is sometimes of type void.
In this section you have learned that expressions are syntactical elements in your code that return a value. It would be an oversimplification to say that the two simple examples I show in this chapter embody all you need to know about expressions, but they do in fact capture many of the key points.
At this stage you hopefully have a general sense of what statements and expressions are all about. A full understanding of these terms will probably only come clear as you read the next few chapters. However, the explanations I've given here should get you started down the road to comprehending expressions and statements.
All of this discussion of expressions and statements is meant merely to set the scene for the next chapter, which is on operators. However, I want to get one more topic out of the way before closing this short chapter. In particular, I want to talk briefly about keywords. This is subject is not as intimately connected to understanding operators as the subject of statements and expressions, but it is nonetheless good to understand this concept before talking in too much depth about the syntax of a language. Unlike statements and expressions, keywords are very simple to understand.
Every language has a certain number of keywords. These are words that the languages uses for its own purposes. For instance, the simple types are all designated by keywords such as char, int, long, etc. These words are reserved for the language itself, and so you can not use keywords as variable names. Keywords cannot be used as identifiers that you create for your own use in your programs.
The following declarations are illegal:
int return; int void; int else; long public; short int;
The problem with all of these declarations is that they declare variables with names that are keywords. For instance, the words return, void, else, public and int are all keywords.
The following declarations are legal:
int a; int Num; int NumHouses; long Count; String MyString;
These statement declarations are legal because the variable names are not keywords. For instance, the identifiers Num, NumHouses and MyString are not keywords.
There is no good way to figure out whether or not a word is keyword. You simply have to memorize the keywords, or else look them up in the online help. It is not, however, really necessary to memorize all the keywords in the language. Instead, you are probably okay if you simply understand what keywords are, so that you can recognize what it is the compiler is complaining about if you accidentally try to use a keyword as a variable name. In particular, misusing a keyword will probably get you an error about "malformed declarations." If you see that error, one possible explanation for why you got it is simple that you are trying to use a keyword as a variable name.
Another way to spot a keyword is by watching the syntax highlighter. If a word appears in bold inside the JBuilder editor, then it is probably a keyword.
Table 1 shows a list of the keywords in the Java language. For now, it is not important that you know what all the keywords mean, but only that you know they exist so that you don't try to use them as variable names in your own programs.
| abstract | else | int | static | boolean | extends | interface | super |
| break | false | long | switch | byte | final | native | synchronized |
| byvalue* | finally | new | this | case | float | null | throw |
| cast* | for | operator* | throws | catch | future* | outer* | transient |
| char | generic* | package | true | class | goto* | private | try |
| const* | if | protected | var* | continue | implements | public | void |
| default | import | rest* | volatile | do | inner* | return | while |
| double | instanceof | short | - | - | - | - | - |
The words with an astarisk after them are reserved, but not currently in use. For instance, there is no goto statement in Java, but the word is reserved nonetheless, on the odd chance that it might be required later.
In this chapter you have learned about statements, expressions and keywords.
Statements are the building blocks out of which you assemble programs. You saw that there are two kinds of statements, simple statements and compound statements. Simple statements express a complete thought, much like a sentence in a piece of prose. You also saw that the best way to get a more precise definition of statements is simply to list all the possible statements available in the Java language, such as assignment statements, if statements, while statements, etc.. Compound statements contain a list of simple statements set off between two matching curly braces.
Expressions return a value. The only odd case is a method call that returns void. This is considered an expression since such a method can be considered as returning the type void. Most expressions are not complete thoughts, but are instead parts of a statement. One exception to this rule is the expression statement. An example of an expression statement is the call to a method.
Keywords are nothing more than a list of words reserved for the use of the creators of the Java language. As such, keywords are not allowed as identifier names for the variables you declare in your programs. The only way to know for sure whether or not a word is a keyword is to look it up in the online help or else memory the list of all keywords. When using JBuilder, however, you can usually spot a keyword because it will be highlighted in bold.
This chapter has been a bit abstract, and you haven't really learned to write any new code by reading it. This text will, nevertheless, hopefully lay the groundwork for the next few chapters, when I discuss operators, and the basic elements of the Java language, such as for statements, while statements, if statements, etc.