Arithmetic Operators

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > Java Fundamentals


Introduction

To solve many programming problems, you will need to use arithmetic operations that manipulate integers and real numbers. Just like an equation, each operator evaluates two operands. The operands may be variables or other expressions. The operators can be used with both integers and real numbers. When the operands are integers, the result is an integer. When the operands are real numbers, a real result is given.


This section will show you how to use arithmetic operators (math operations) in Java. See page 54 in the Gaddis text: http://tinyurl.com/gaddis54


Table of Arithmetic Operators

Operator Meaning Example
+ Addition 6 + 9 is 15.
- Subtraction 19 - 5 is 14.
* Multiplication 6 * 5 is 30.
/ Division 4 / 2 is 2.
% Remainder 5 % 2 is 1.


Data Type Precedence

When both an integer and a double (a real number) are used in an arithmetic operation, the double will take precedence. This means the result of any operation using a double will also be a double.

Example :

 int test1 = 9;
double test2 = 8.7;

test1 + test2 would equal 17.7. 

This rule also applies to all other operators. A double will always take precedence over an integer making the result a real number. What it comes down to is: "The type of the result of an arithmetic operation is double if an operand is type double. If both operands are type int, the result is type int."


Assignment Statements

Normally, an assignment statement such as

 x = y + z; 

is used to store an arithmetic expression (y + z) in a primitive type variable x. The symbol = is the assignment operator. This statement should be thought of as 'x gets the value of y plus z'. Remember, arithmetic operators can only be applied to primitive data types.

In Java, you can also write assignment statements with the form

 x = x + y; 

where x appears on both sides of the assignment operator. This is a common programming practice which adds the value y to x and stores the result back in x. If we had

 x = 2;
y = 4;

x = x + y; 

x would now have the value 6.


Mixed-Type Assignment Statement

When an assignment statement with multiple operators is executed, the expression evaluated and then the result is assigned to the variable preceding the assignment operator. When assigning a result to a variable of the type double, the result can be either a real number or an expression (integer '2', would be changed to '2.0').

Example 6:

 int j = 6;
int k = 5;

double l = j + k;  //assigns 11 to l, stored as a real number (11.0)
double x = l + j / k; //assigns 17.0 to x 

Notice how 'j + k' is done first, before the addition of l and j. j and k remain integers and this statement evaluates to 11. Since the result is being assigned to a double, it is converted to 11.0. l is then added to j and the result 17.0.


Expressions with Multiple Operators

Java uses the same rules as algebra to determine the order of operator evaluation. How do you remember it? PEMDAS? BEMDAS? Please Exucse My Dear Aunt Sara? Any way you remember it, this is the order:

  • Parenthesis (Brackets)
  • Exponents
  • Multiplication and Division
  • Addition and Subtraction

Also, remember,

a. Parentheses rule: All expressions in brackets are evaluated separately. Nested parentheses work from the inside out, with the inner most expressions evaluated first.

b. Operator rule: Operators in the same expression are evaluated in the order determined by their precedence (from highest to lowest).

 x * y + z / a - c * e + v % q 

can be written more clearly as

 (x * y) + (z / a) - (c * e) + (v % q) 


Summary

You should now know how to use all of the basic arithmetic operators. With this new knowledge you should be able to begin solving more complex programming problems.

Previous Page: Common Primitive Variables Next Page: Relational Operators