Arithmetic Operators

   

Java™ 2 Primer Plus
By Steven Haines, Steve Potts

Table of Contents
Chapter 3.  Operators


Arithmetic operators refer to the standard mathematical operators you learned in elementary school: addition, subtraction, multiplication, and division.

Addition

Addition, as you would expect, is accomplished using the plus sign (+) operator. The form of an addition operation is

 operand + operand 

For example:

 // Add two literal values  int result = 5 + 5;  // Add two variables  int a = 5;  int b = 6;  int result = a + b;  // Add two variables and a literal  int result = a + b + 15; 

An addition operation, can add two or more operands, whereas an operand can be a variable, a literal, or a constant.

Subtraction

Subtraction, again as, you would expect, is accomplished using the minus sign ( ) operator. The form of a subtraction operation is

 operand - operand 

For example:

 // Subtract a literal from a literal; the result is 5  int result = 10 - 5;  // Subtract a variable from another variable; the result is -1  int a = 5;  int b = 6;  int result = a - b;  // Subtract a variable and a literal from a variable  // The result is 5   6   15 = -1   15 = -16  int result = a - b - 15; 

A subtraction operation can compute the difference between two or more operands, where an operand can be a variable, a literal, or a constant.

Multiplication

Multiplication is accomplished using the asterisk (*) operator. The form of a multiplication operation is

 operand * operand 

For example:

 // Multiply two literal values; result is 25  int result = 5 * 5;  // Multiply two variables; result is 30  int a = 5;  int b = 6;  int result = a * b;  // Multiply two variables and a literal  // The result is 5 * 6 * 15 = 30 * 15 = 450  int result = a * b * 15; 

A multiplication operation can multiply two or more operands, where an operand can be a variable, a literal, or a constant.

Division

Division is accomplished using the forward slash (/) operator. The form of a division operation is

 operand / operand 

For example:

 // Divide a literal by a literal; result is 5  int result = 10 / 2;  // Divide a variable by another variable; result is 3  int a = 15;  int b = 5;  int result = a / b; 

When dividing integer types, the result is an integer type (see the previous chapter for the exact data type conversions for mathematical operations). This means that if you divide an integer unevenly by another integer, it returns the whole number part of the result; it does not perform any rounding. For example, consider the following two operations that both result to 1.

 int result1 = 10 / 6; // Float value would be 1.6666  int result2 = 10 / 9; // Float value would be 1.1111 

Both result1 and result2 resolve to be 1, even though result1 would typically resolve to 2 if you were rounding off the result. Therefore, be cognizant of the fact that integer division in Java results in only the whole number part of the result, any fractional part is dropped.

When dividing floating-point variables or values, this caution can be safely ignored. Floating-point division results in the correct result: The fractional part of the answer is represented in the floating-point variable.

 float f = 10.0f / 6.0f;  // result is 1.6666  double d = 10.0 / 9.0; // result is 1.1111 

Note the appearance of the f following each literal value in the first line. When creating a floating-point literal value (a value that has a fractional element), the default assumption by the compiler is that the values are double. So, to explicitly tell the compiler that the value is a float and not a double, you can suffix the value with either a lowercase or uppercase F.

Modulus

If integer division results in dropping the remainder of the operation, what happens to it? For example if you divide 10 by 6:

 int i = 10 / 6; 

The Java result is 1, but the true result is 1 Remainder 4. What happened to the remainder 4?

Java provides a mechanism to get the remainder of a division operation through the modulus operator, denoted by the percent character (%). Although the previous example had a result of 1, the modulus of the operation would give you that missing 4. The form of a modulus operation is

 operand % operand 

For example:

 int i = 10 / 6; // i = 1  int r = 10 % 6; // r = 4 

Similar to the other arithmetic operators in this chapter, the modulus of an operation can be performed between variables, literals, and constants.


       
    Top
     



    Java 2 Primer Plus
    Java 2 Primer Plus
    ISBN: 0672324156
    EAN: 2147483647
    Year: 2001
    Pages: 332

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net