Header Ads

Breaking News
recent

Mixed Expressions and Allocating Memory with Named Constants and Variables

An expression that has operands of different data types is called a mixed expression. A mixed expression contains both integers and floating-point numbers. The following expressions are examples of mixed expressions:
2 + 3.5
6 / 4 + 3.9
5.4 * 2 - 13.6 + 18 / 2
In the first expression, the operand + has one integer operand and one floating-point operand. In the second expression, both operands for the operator / are integers; the first operand of + is the result of 6 / 4, and the second operand of + is a floating-point number. The third example is a more complicated mix of integers and floating-point numbers. How does Java evaluate such mixed expressions?

Two rules apply when evaluating a mixed expression:
1. When evaluating an operator in a mixed expression:

a. If the operator has the same types of operands (that is, both are integers or both are floating-point numbers), the operator is evaluated according to the type of the operand. Integer operands yield an integer result; floating-point numbers yield a floating-point number result.
b. If the operator has both types of operands (that is, one is an integer and the other is a floating-point number), during calculation the integer is treated temporarily as a floating-point number with the decimal part of zero, and then the operator is evaluated. The result is a floating-point number.

2. The entire expression is evaluated according to the precedence rules. The multiplication, division, and modulus operators are evaluated before the addition and subtraction operators. Operators having the same level of precedence are evaluated from left to right. Grouping is allowed for clarity.

Following these rules, when you evaluate a mixed expression, you concentrate on one operator at a time, using the rules of precedence. If the operator to be evaluated has operands of the same data type, evaluate the operator using Rule 1(a). That is, an operator with integer operands yields an integer result, and an operator with floating-point operands yields a floatingpoint result. If the operator to be evaluated has one integer operand and one floating-point operand, before evaluating this operator, you treat the integer operand as a floating-point number with a decimal part of zero. Example shows how to evaluate mixed expressions.

Mixed Expression Evaluation Rule Applied
3 / 2 + 5.0 = 1 + 5.0 3 / 2 = 1 (integer division; Rule 1(a))
= 6.0 (1 + 5.0 = 1.0 + 5.0 (Rule 1(b))
= 6.0)
15.6 / 2 + 5 = 7.8 + 5 15.6 / 2 = 15.6 / 2.0 (Rule 1(b))
= 7.8
= 12.8 7.8 + 5 = 7.8 + 5.0 (Rule1(b))
= 12.8
4 + 5 / 2.0 = 4 + 2.5 5 / 2.0 = 5.0 / 2.0 (Rule 1(b))
= 2.5
= 6.5 4 + 2.5 = 4.0 + 2.5 (Rule 1(b))
= 6.5
4 * 3 + 7 / 5 - 25.5 = 12 + 7 / 5 - 25.5 4 * 3 = 12; (Rule 1(a))
= 12 + 1 - 25.5 7 / 5 = 1 (integer division; Rule 1(a))
= 13 - 25.5 12 + 1 = 13; (Rule 1(a))
= -12.5 13 - 25.5 = 13.0 - 25.5 (Rule 1(b))
= -12.5


Allocating Memory with Named Constants and Variables

When you instruct the computer to allocate memory, you tell it what names to use for each memory location and what type of data to store in those locations. Knowing the location of the data is essential because data stored in one memory location might be needed at several places in the program. As you learned earlier, knowing what data type you have is crucial for performing accurate calculations. It is also critical to know whether your data must remain constant throughout program execution or whether it could change.

Some data must not be changed. For example, the pay rate might be the same for all parttime employees. The value in a conversion formula that converts inches into centimeters is fixed, because 1 inch is always equal to 2.54 centimeters. When stored in memory, this type of data must be protected from accidental changes during program execution. In Java, you can use a named constant to instruct a program to mark those memory locations in which data is constant throughout program execution.
Named constant: A memory location whose content is not allowed to change during
program execution.

To allocate memory, we use Java’s declaration statements. The syntax to declare a named constant is:

In Java, static and final are reserved words. The reserved word final specifies that the value stored in the identifier is fixed and cannot be changed. In syntax, the shading indicates the part of the definition that is optional. Because the reserved word static is shaded, it may or may not appear when a named constant is declared. The section, Creating a Java Application Program. Also, notice that the identifier for a named constant is in uppercase letters. This is because Java programmers typically use uppercase letters for a named constant.

No comments:

Powered by Blogger.