Increment and Decrement Operators

Java provides two unary operators for adding 1 to or subtracting 1 from the value of a numeric variable. These are the unary increment operator, ++, and the unary decrement operator, --, which are summarized in Fig. 4.15. A program can increment by 1 the value of a variable called c using the increment operator, ++, rather than the expression c = c + 1 or c += 1. An increment or decrement operator that is prefixed to (placed before) a variable is referred to as the prefix increment or prefix decrement operator, respectively. An increment or decrement operator that is postfixed to (placed after) a variable is referred to as the postfix increment or postfix decrement operator, respectively.

Figure 4.15. Increment and decrement operators.

Operator

Called

Sample
expression

Explanation

++

prefix
increment

++a

Increment a by 1, then use the new value of a in the expression in which a resides.

++

postfix
increment

a++

Use the current value of a in the expression in which a resides, then increment a by 1.

--

prefix
decrement

--b

Decrement b by 1, then use the new value of b in the expression in which b resides.

--

postfix
decrement

b--

Use the current value of b in the expression in which b resides, then decrement b by 1.

Using the prefix increment (or decrement) operator to add (or subtract) 1 from a variable is known as preincrementing (or predecrementing) the variable. Preincrementing (or predecrementing) a variable causes the variable to be incremented (decremented) by 1, and then the new value of the variable is used in the expression in which it appears. Using the postfix increment (or decrement) operator to add (or subtract) 1 from a variable is known as postincrementing (or postdecrementing) the variable. Postincrementing (or postdecrementing) the variable causes the current value of the variable to be used in the expression in which it appears, and then the variable's value is incremented (decremented) by 1.

Good Programming Practice 4.7

Unlike binary operators, the unary increment and decrement operators should be placed next to their operands, with no intervening spaces.

Figure 4.16 demonstrates the difference between the prefix increment and postfix increment versions of the ++ increment operator. The decrement operator (--) works similarly. Note that this example contains only one class, with method main performing all the class's work. In this chapter and in Chapter 3, you have seen examples consisting of two classesone class containing methods that perform useful tasks and one containing method main, which creates an object of the other class and calls its methods. In this example, we simply want to show the mechanics of the ++ operator, so we use only one class declaration containing method main. Occasionally, when it does not make sense to try to create a reusable class to demonstrate a simple concept, we will use a mechanical example contained entirely within the main method of a single class.

Figure 4.16. Preincrementing and postincrementing.

 1 // Fig. 4.16: Increment.java
 2 // Prefix increment and postfix increment operators.
 3
 4 public class Increment
 5 {
 6 public static void main( String args[] )
 7 {
 8 int c;
 9
10 // demonstrate postfix increment operator
11 c = 5; // assign 5 to c
12 System.out.println( c ); // print 5
13 System.out.println( c++ ); // print 5 then postincrement
14 System.out.println( c ); // print 6 
15
16 System.out.println(); // skip a line
17
18 // demonstrate prefix increment operator
19 c = 5; // assign 5 to c
20 System.out.println( c ); // print 5
21 System.out.println( ++c ); // preincrement then print 6
22 System.out.println( c ); // print 6 
23
24 } // end main
25
26 } // end class Increment
 
5
5
6

5
6
6
 

Line 11 initializes the variable c to 5, and line 12 outputs c's initial value. Line 13 outputs the value of the expression c++. This expression postincrements the variable c, so c's original value (5) is output, then c's value is incremented. Thus, line 13 outputs c's initial value (5) again. Line 14 outputs c's new value (6) to prove that the variable's value was indeed incremented in line 13.

Line 19 resets c's value to 5, and line 20 outputs c's value. Line 21 outputs the value of the expression ++c. This expression preincrements c, so its value is incremented, then the new value (6) is output. Line 22 outputs c's value again to show that the value of c is still 6 after line 21 executes.

The arithmetic compound assignment operators and the increment and decrement operators can be used to simplify program statements. For example, the three assignment statements in Fig. 4.12 (lines 27, 29 and 32)

 passes = passes + 1;
 failures = failures + 1;
 studentCounter = studentCounter + 1;

can be written more concisely with compound assignment operators as

 passes += 1;
 failures += 1;
 studentCounter += 1;

with prefix increment operators as

 ++passes;
 ++failures;
 ++studentCounter;

or with postfix increment operators as

 passes++;
 failures++;
 studentCounter++;

When incrementing or decrementing a variable in a statement by itself, the prefix increment and postfix increment forms have the same effect, and the prefix decrement and postfix decrement forms have the same effect. It is only when a variable appears in the context of a larger expression that preincrementing and postincrementing the variable have different effects (and similarly for predecrementing and postdecrementing).

Common Programming Error 4.9

Attempting to use the increment or decrement operator on an expression other than one to which a value can be assigned is a syntax error. For example, writing ++(x + 1) is a syntax error because (x + 1) is not a variable.

Figure 4.17 shows the precedence and associativity of the operators we have introduced to this point. The operators are shown from top to bottom in decreasing order of precedence. The second column describes the associativity of the operators at each level of precedence. The conditional operator (?:); the unary operators increment (++), decrement (--), plus (+) and minus (-); the cast operators and the assignment operators =, +=, -=, *=, /= and %= associate from right to left. All the other operators in the operator precedence chart in Fig. 4.17 associate from left to right. The third column names the groups of operators.

Figure 4.17. Precedence and associativity of the operators discussed so far.

(This item is displayed on page 157 in the print version)

Operators

         

Associativity

Type

++

--

       

right to left

unary postfix

++

--

+

-

( type )

 

right to left

unary prefix

*

/

%

     

left to right

multiplicative

+

-

       

left to right

additive

<

<=

>

>=

   

left to right

relational

==

!=

       

left to right

equality

?:

         

right to left

conditional

=

+=

-=

*=

/=

%=

right to left

assignment


Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

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