What Is an Expression?

   

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
Name of Expression Example
Primary expression x
Additive expression x + 5
Assignment expression x = 5
Array indexing sizes[11]
Method invocation triangle.rotateLeft(50)
   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

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