Learn-Java.com

  • Increase font size
  • Default font size
  • Decrease font size
Home Arithmetic Operators

Arithmetic Operators

E-mail Print PDF

To solve many programming problems, you will need to use arithmetic operations that manipulate integers and real numbers. Each operator evaluates two operands, which 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.

Table of Arithmetic Operators

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

 

Data Type of an Arithmetic Operator

One thing to note is the use of both an integer and a real number in an arithmetic operation. If we were to add an integer and a real number together, the result would be a real number.

Example 1:

int test1 = 9;
double test2 = 8.7;


test1 + test2 would equal 17.7.

This applies to all other operators as well. A double will take precedence over an int, so the result will always be a real number. What it basically 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.

Example 2:
Form: variable = expression;
Example: y = t - u;

The data types of expression and variable must be the same (or you will get an error when you compile). Using the previous example, we could use a double data type, result, to hold the answer.

Example 3:

doubel result;
int test1 = 9;
double test2 = 8.7;
result = test1 + test2;    //we can then print out this statement, or use it in some other function

 


Example 4:
In Java you can write assignment statements with the form
sum = sum + item;
where sum appears on both sides of the assignment operator. This is a common programming practice, and instructs the computer to take the current value of sum, then add the value in item, and take this total and store it in sum.

Example 5:
You can also use assignment statements to assign the value of one variable to another variable.
newZ = z;
copies the value of variable z into variable newZ. You can also negate the statement, before assigning it newZ.
newZ = -z;
This instructs the computer to get the value of z, negate it, then assign the negated value to newZ.
Mixed-Type Assignment Statement
When an assignment statement with multiple operators is executed, the expression is first evaluated and then the result is assigned to the variable preceding the assignment operator. Either a real number or an expression may be be assigned to a type double variable (integer '2', can 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 12.0 to x

Notice how 'j / k' is done first, before the addition. j and k are still both integers, and this statement evaluates to 1. This value is then converted to a double (1.0), and added onto l (11.0).

Expressions with Multiple Operators

Java uses the same rules as algebra to determine the order of operator evaluation.

Rules for Evaluating Expressions
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).

Operator Precedence
- Highest Precedence
*, /, %
+, -
= Lowest Precedence



Example 7:
x * y + z / a - c * e + v % q
can be written more clearly as

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

Last Updated on Wednesday, 02 December 2009 09:24  

Search This Site


Newsflash


"Welcome to Learn Java.com. A learning website created to teach programming concept through Java. Learn java.com is a beginner guide to programming and it is also an introductory guide to Java."