Expressions ”combinations of operators and operands ”are one of the key building blocks of the Java language, as they are of most any programming language. Expressions allow you to perform arithmetic calculations, compare values, perform logical operations, and manipulate objects. Without expressions, a programming language would be of little use to you. You've already seen some expressions, mostly fairly simple ones, in other chapters in this book. Chapter 3, "Data Types and Other Tokens," showed you that the operators used in expressions form one of the main classifications of Java tokens, along with others such as keywords, comments, and so on. In this chapter, you get a closer look at how you can use operators to build expressions ”in other words, how to put operators to work for you. You can define what an expression is in technical terms, but at its simplest, an expression is a sequence of one or more operands and operators that are evaluated by the Java interpreter when your code is executed, or by the compiler if the result can be computed in advance. An expression might be used simply to state the value of a variable or a literal without the use of any operators. However, expressions are more interesting when they contain operators that manipulate variables and do work for you. Table 5.1 shows several legal Java expressions. Table 5.1. Examples of Java Expressions
|