Richard G Baldwin (512) 223-4758, baldwin@austin.cc.tx.us, http://www2.austin.cc.tx.us/baldwin/

Expressions

Java Programming, Lecture Notes # 24, Revised 10/03/99.

Preface
Introduction
Expressions
Review

Preface

Students in Prof. Baldwin's Introductory Java Programming classes at ACC are responsible for knowing and understanding all of the material in this lesson (except that they are not responsible for detailed information that is specific to C++).

The detailed material on C++ is provided as supplementary material for the benefit of those persons who are already familiar with C++ and who are making the transition into Java.

Introduction

The first step

The first step in learning to use a new programming language is usually to learn the foundation concepts such as variables, types, expressions, flow-of-control, etc. This lesson concentrates on expressions.

A very short lesson

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.

Expressions

The hierarchy

Java and C++ programs are composed of statements, and statements are constructed from expressions.

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.

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.

Named constants

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.

The syntax for creating a named constant in Java is as follows:

final float PI = 3.14159;

While this is not a constant type, it does produce a value that can be referenced in the program and which cannot be modified.

The final keyword

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.

Operator precedence

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.

Review

Q - A Java program is composed of a series of what?

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.

-end-