Lesson 3 - Math Functions, Constants


The JAVA language provides some unusual operators called assignment operators. These shortcuts allow you to write shorter source code and they allow the compiler to create more efficient runtime code. Because of the abundance of operators in this language, the operator precedence table in JAVA is a long one. We begin our study of operator precedence with the familiar math operators.

In keeping with the concept of recycling code, we will examine some of the functions available in the Math class. A fundamental set of math methods are pre-defined in JAVA and ready for use.

The key topics for this lesson are:

A.  Standard Math Methods
B.  Precedence of Math Operators
C.  Assignment Operators
D.  Increment Operators
E.  Named Constants





A. Standard Math Methods

1. The Math class in the java.lang package contains class methods for commonly used mathematical functions. Java loads the java.lang package automatically, so no special actions are required to access these.

2. For example, to express the mathematical formula

      0.5 sin ( x - pi / y^3 )
Using the Math class constant and methods would give
      (1.0/2.0) * Math.sin(x  Math.PI / Math.pow(y, 3) );  
Notice how the class methods and class constants are referred to in the expression. The syntax is
	<class name>.<method name> ( <arguments> );  
	<class name>.<class constant>;  

B.  Precedence of Math Operators

1. Precedence rules govern the order in which an expression is solved. For example:

	2 + 3 * 6 = 20       The * operator has priority over +.  

2. Associativity refers to the order in which operators are applied if they have the same precedence level. The two possibilities are from left-to-right or right-to-left.

3. The following table summarizes precedence and associativity of math operators:

Level of Precedence Operator Associativity
Highestunary - right to left
 * / % left to right
Lowest+ - left to right

4. An example follows:

9 + 5 * 7 % 8 - 5 (solve * first)
9 + 35 % 8 - 5 (solve % next)
9 + 3 - 5 (solve left-to-right)

5. Parentheses take priority over all the math operators.

      (5+6)/(9-7) = 11/2 = 5   (integer division is used here)  

6. JAVA has a long list of operators for which there are many different levels of precedence. A partial list of the operators in JAVA have been summarized in Lesson 6, organized by precedence level.

C.  Assignment Operators

1. The statement     number = number + 5;      is an example of an accumulation statement. The old value of number is incremented by 5 and the new value is stored in number.

2. The above statement can be replaced as follows:

	number += 5;  

3. Java provides for the following assignment operators:

        +=       -=       *=       /=       %=  

4. The following examples are equivalent statements:

        rate *= 1.05;        rate = rate * 1.05;
        sum += 25;           sum = sum + 25;
        number %= 5;         number = number % 5;  

5. The precedence of the assignment operators is the lowest of all operators.

D.  Increment Operators

1. Incrementing or decrementing by one is a common task in programs. This can be solved by the statements:

        n = n + 1;     or     n += 1;  

2. Java also provides a unary operator called an increment operator, ++.

3. The statement n = n + 1 can be rewritten as ++n. The following statements are equivalent:

        n = n + 1;          ++n;
        sum = sum + 1;      ++sum;  

4. JAVA also provides for a decrement operator, --, which decrements a value by one. The following are equivalent statements:

        n = n - 1;          --n;
        sum = sum - 1;      --sum;  

5. The increment and decrement operator can be written as either a prefix or postfix unary operator. If the ++ is placed before the variable it is called a prefix increment operator (++number), but it can follow after the variable (number++), which is called a postfix increment operator. The following three statements have the same effect:

        ++number;      number++;      number = number + 1;  

6. Before we look at the difference between prefix and postfix unary operators it is important to remember Java operators solve problems and often return values. Just as the assignment operator (=) returns a value, the ++ and -- operators return values. Consider the following code fragments:

int a=1, b;

b = ++a;

After execution of the code

b = ++a;

a = 2 and b = 2
int a=1, b;

b = a++;

After execution of the code

b = a++;

a = 2 and b = 1

7. The statement b = ++a uses the preincrement operator. It increments the value of a and returns the new value of a.

8. The statement b = a++ uses the postincrement operator. It increments the value of a and returns the old value of a.

9. The precedence and associativity of the unary increment and decrement operators is the same as the unary - operator.

E.  Named Constants

1. Java allows for declaration of named constants in a program. The general syntax is:

        final <data type> <CONSTANT_NAME> = <expression>;  
a.  final is a Java reserved word
b.  <data type> is any previously defined type
c.  <CONSTANT_NAME> is a valid Java identifier
d.  <expression> is any valid expression of the appropriate type

2. Examples:

        final double PI = 3.14159;
        final double GAS_CONSTANT = 8.206E-2D;
        final short DAY_IN_WEEK = 7;
        final char BIG_J = 'J';  

3. Some programmers declare constant identifiers using all uppercase letters. This distinguishes such identifiers from variables. This curriculum guide will use uppercase identifiers for constant values.

4. The major benefit of using constant values occurs when such constants need to be modified. Suppose you have a program which uses a tax rate value in 20 different lines of code. If you had used the literal value of 0.0525, you would need to change the value in 20 different spots. However, if you had declared a constant identifier and used the identifier in those 20 spots, then one change at the top of the program would update the value throughout the program.

        final double TAXRATE = 0.0825;  
All references to TAXRATE are now changed.


Your lab work will incorporate the material from the previous lessons. The worksheet will provide practice on operator precedence and assignment operators.

Back to Java Main Page