THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

Expressions where the value assigned to a variable is calculated from the original value of the variable are
common enough that there is a short-hand for writing them. For example, another way to write i=i+ 1 is to
write


i += 1;


which adds the value on the right-hand side of the += operator (namely 1 ) to the variable on the left-hand side
(namely i). Most of the binary operators (operators that take two operands) can be joined with = in a similar
way (such as +=, -=, *=, and /=).


Inside the for loop body we use an ifelse statement to see whether the current hi value is even. The if
statement tests the boolean expression between the parentheses. If the expression is TRue, the statement
(which can be a block) in the body of the if is executed. If the expression is false, the statement in the
body of the else clause is executed. The else part is optional. If the else is not present, nothing is done
when the expression is false. After figuring out which (if any) clause to execute, and then actually
executing it, control passes to the code following the body of the if statement.


The example tests whether hi is even using the %, or remainder, operator (also known as the modulus
operator). It produces the remainder after dividing the value on the left side by the value on the right. In this
example, if the left-side value is even, the remainder is zero and the ensuing statement assigns a string
containing the even-number indicator to mark. The else clause is executed for odd numbers, setting mark
to an empty string.


The println invocations appear more complex in this example because the arguments to println are
themselves expressions that must be evaluated before println is invoked. In the first case we have the
expression "1: "+lo, which concatenates a string representation of lo (initially 1 ) to the string literal "1:
"giving a string with the value "1: 1". The + operator is a concatenation operator when at least one of its
operands is a string; otherwise, it's an addition operator. Having the concatenation operation appear within the
method argument list is a common short-hand for the more verbose and tedious:


String temp = "1: " + lo;
System.out.println(temp);


The println invocation within the for loop body constructs a string containing a string representation of
the current loop count i, a separator string, a string representing the current value of hi and the marker string.


Exercise 1.7: Change the loop in ImprovedFibonacci so that i counts backward instead of forward.


1.7. Classes and Objects


The Java programming language, like many object-oriented programming languages, provides a tool to solve
programming problems using the notions of classes and objects. Every object has a class that defines its data
and behavior. Each class has three kinds of members:


Fields are data variables associated with a class and its objects. Fields store results of computations
performed by the class.


Methods contain the executable code of a class. Methods are built from statements. The way in which
methods are invoked, and the statements contained within those methods, are what ultimately directs
program execution.

Free download pdf