Java Reference
In-Depth Information
ecute, and then actually executing it, control passes to the code follow-
ing the body of the if statement.
The example tests whether hi is even using the % , or remainder, oper-
ator (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 en-
suing 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 eval-
uated 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 + op-
erator 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 con-
taining a string representation of the current loop count i , a separat-
or 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.
 
Search WWH ::




Custom Search