When a method accepts parameters, we have already seen that the precise data type for each parameter is specified in the method definition. For example, the method declared as:
void Shape.setBorderThickness(int newThickness)expects to be sent a single parameter which must be of data type int.
Similarly, we have seen that when a variable is declared, the precise data type must be declared. For example, we might declare a variable as:
int myChoice;If you wish to set the value of such a variable, you can do so with an assignment statement, such as
myChoice = ... ;so long as the right hand side ("...") is of the proper data type (int).
For each of the above, there are three possible ways in which you can specify the desired int value.
Literals
You can explicitly state the desired value, for example in the
assignment statement
myChoice = 5;or in the method call
myshape.setBorderThickness(5);In both of these cases, the occurrence of 5 is as a literal of type int.
Variables
If you already had a variable declared of type int, you
could substitute the variable's identifier in place of its
value. For example,
int myChoice = 5;
myshape.setBorderThickness(myChoice); // note the use of the variable identifier, myChoice
Expressions
More generally, you can use operators to build more
complex expressions which can be used to specify a
desired value.
For example, the operator, +, in the context of data type int signifies that an addition should be performed. For example, either of the following are legal statements:
myshape.setBorderThickness(2+3);For a binary operator, such as addition, there are two operands. The expression will be well-formed so long as each of the operands are themselves expressions of the expected data types. Therefore, you can create more complicated integer expressions, such as: ((2+(myChoice/2)) - 3)
myshape.setBorderThickness(myChoice+1);
Note that parentheses can be used to explicitly control the evaluation of the expression. In particular, the part of an expression within the parentheses will be evaluated before operators which are outside of the parentheses. However, you can also give expressions which are not fully parenthesized, such as 3+5*4-8/2. In this case, Java will rely on a predefined precedence order for evaluating the various operators, in this case, evaluating the expression as (3+(5*4))-(8/2) because multiplication and division are given higher precedence than addition and subtraction. Operators of equal value, they are processed from left to right (e.g., the addition takes place before the subtraction in this example)
Here, we give a table highlighting the various data types we discussed, as well as the syntax for literals of that type and available operators and precedence order. Details are given in the reading from the text.
| Data Type: | int | double | char | String | boolean |
|---|---|---|---|---|---|
| Literals: | 5, 23 | 3.14, 5.0 | 'A', '@' | "How are you?" | true, false |
|
Operators (from high to low precedence): |
|
|
+ (concatenation) |
|
Relational Operators can be used to compare a pair of values
from another type, yielding a boolean result.
<
<=
>
>=
!=
==
In the case of numeric data, these operators have the standard
interpretation. They can be used for char or
String as well, in which case the inequalities are
evaluated based upon lexicographical (i.e., dictionary) order
according to the UNICODE encoding of characters.
(one additional subtlety arises involving the use of equality
with String objects, but we will come back to this another time.)
Assignment Operator (=)
[ not to be confused with the Equality
Operator (==) ]
The assignment operator is a special operator.
Consider the statement
The assignment operator is the lowest priority operator. Therefore when it is encountered, the entire right-hand side of the expression will first be evaluated, and then the resulting value is assigned to the left-hand side. The left-hand side cannot be a general expression, rather it must be a variable of a data type which matches the result of the right-hand side.
Because of this semantics, it is possible to use an assignment
statement to change a variable value, even though the variable's
current value is used in the right-hand side, e.g.,
Additional operators
In fact, it is so common to want to alter a variable based on
its own current value, there are special assignment variables
for just this purpose. For example, the statement
An even more common instruction is to add one to (increment) or subtract one from (decrement) a given numeric variable. Four additional operators are defined, as discussed in Table 5.7 of the text, based on the syntax ++ or -- placed immediately before or after a variable.
A method call can itself be used as part of an expression, based upon the data type of its return value. For example, our Circle class supports a method getRadius() which returns an int value. Therefore, in building a snowman, we might create circles beginning the following code:
Circle head;
Circle middle;
Circle bottom;
head = new Circle(15);
middle = new Circle(2*head.getRadius()); // NOTE WELL
bottom = new Circle(2*middle.getRadius()); // NOTE WELL
Type Conversions and Casts (translating from one data type to another)