Except for the use of different operators in some cases, there is very little difference in expressions between Java and other languages such as C, C++, Pascal, and BASIC. Since you are already expected to understand how to construct expressions in Pascal or some other modern structured programming language, this lesson will be very short.
A method call evaluates to the value returned by the method, and the type of a method call is the type returned by the method.
Java supports named constants that are implemented through use of the
final
keyword.
| C++ does not support true constants except for manifest constants implemented by way of #define statements to the preprocessor. Also, in C++, the const keyword can sometimes be used to obtain the effect of a constant. |
final float PI = 3.14159; |
It is the keyword final which prevents the value of PI from being modified in this case. You will learn later that there are some other uses for the final keyword in Java as well.
In some cases, the order in which the operations are performed determines
the result. As in C++ (and Pascal and other languages as well), you can
control the order of evaluation by the use of balanced parentheses. If
you don't provide such parentheses, the order will be determined by the
precedence of the operators (you should find and review a table of operator
precedence) with the operations having higher precedence being evaluated
first.
| Note that except for use in the first clause of a for statement, Java does not support the comma operator which is available in C++ to control the order in which expressions are evaluated. |
A - A Java program is composed of a series of statements.
Q - Statements in Java are constructed from what?
A - Statements in Java re constructed from expressions.
Q - Describe an expression in Java.
A - An expression is a specific combination of operators and operands which evaluates to a particular result. The operands can be variables, constants, or method calls.
Q - What does a method call evaluate to in Java?
A - A method call evaluates to the value returned by the method.
Q - What is the type of a method in Java?
A - The type of a method in Java is the type of result returned by the method.
Q - Java supports named constants: True or False? If false, explain why.
A - True. Java supports named constants that are constructed using variable declarations with the final keyword.
Q - Provide a code fragment that illustrates the syntax for creating a named constant in Java.
A - The syntax for creating a named constant in Java is as follows:
final float PI = 3.14159;Q - Java supports a constant type: True or False. If false, explain why.
A - Java does not support a constant type. However, in Java, it is possible to achieve the same result by declaring and initializing a variable and making it final.
Q - What is the common method of controlling the order of evaluation of expressions in Java?
A - As in C++, Pascal, and other languages as well, you can control the order of evaluation by the use of balanced parentheses.
Q - If you don't use balanced parentheses to control the order of evaluation of expressions, what is it that controls the order of evaluation?
A - If you don't provide such parentheses, the order will be determined by the precedence of the operators with the operations having higher precedence being evaluated first.