2.2 Assigning and Printing Expressions

In the previous chapter, we saw how we could give values to variables using
assignment statements of the form

<identifier> = <value>;

As you might expect, assignment statements can be extended to give values
of expressions to variables by using statements of the form

<identifier> = <expression>;

In carrying out such assignments, the expression on the right is evaluated
and then the resulting value is stored in the memory location of the
variable on the left. Of course, this will destroy any previous value that
may have been stored in that location.
The type of the expression on the right and that of the variable on the
left are usually identical but, if they are not, then the rules that we saw in
the last chapter for making conversions apply here also.
If we are working with values of various types, we may want to use
the operation of performing a cast that we studied in the last chapter. A
cast operator is applied to the value to the immediate right of the cast. A
cast operator has a higher precedence than either *, /, or %. Thus, if it is
combined with other operations and we want it to apply to more than one
value in an expression, we should use parentheses.

Example 1

(a) (int) 5.7 + 8.9 =} 5 + 8.9 =} 13.9   ( a double value)
(b) (int) 5.7 + (int) 8.9 =} 5 + 8 =} 13   (an int value)
(c) (int) (5.7 + 8.9) =} (drrt ) (14.6)=}14 (an int value)
(d) (double) (1/2) =} (double) a=} 0.0  (a double value)
(e) (double) 5/0 =} 5. a/a   (produces positive infinity)
(f) (double) (5/0)  (throws an ArithmeticException)

We can use the print or println methods to print values of expressions, with or without accompanying strings. In using arithmetic expressions as parts of the argument to print or println, we must be careful of the fact that the operator + has different meanings for string expressions (where it means concatenation) and numerical expressions (where it means addition). 1 Whichever way we use +, it has the same precedence as the arithmetic operator and if there is more than one occurrence of + in an expression, evaluation takes place from left to right unless parentheses are used to alter the order of evaluation. If + is used between a string and a primitive value, the primitive value is automatically converted to a string and then the two strings are concatenated. Using these rules, Java forms a single string argument for print or println and then prints this string. The next example illustrates the use of + in a variety of contexts.

Example 2

Suppose that the following declarations have been made:

int m = 3, n = 4;

The following table shows the output of a variety of statements involving these variables. Can you explain each line of output?

Exercise 2.2

  1. Find the value of each expression. Use a decimal point in your answer if the result is a floating point value.
    (a) (int) 1.8 * 0.6	(b) (int) (2/0.9)
    (c) 6 / 5 % (double) 2	(d) 2* (double) (7/4)
    (e) (int)6.3 - (int)4.7	(f) (int)6.3 - 4.7
    (g) (int)(6.3 - 4.7)	(h) (double) 3 * 5 / 0
    (i) (float) 2*3 + 0.1	(j) (double) (7 % 4 / 3)
    
  2. Assume that a program contains the following declarations.

    int i = 5, j = 2j

    What will be printed by each statement?
    (a) System.out.println(i + j + 2);
    (b) System.out.println(i + " + " + j + 2);
    (c) System.out.println(i * j + 2);
    (d) System.out.println("i * j " + 2);
    
  3. Assume that a program contains the following declarations.

    int i = 4, j = 7;

    What will be printed by each statement?
    (a) System.out.println("i " + i);
    (b) System.out.println(i + j);
    (c) System.out.println("i + j -> " + i + j);
    (d) System.out.println(i + j + " <- i + j");
    (e) System.out.println("i * j -> " + i * j);