|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The Java programming language supports various arithmetic operators for all floating-point and integer numbers. These operators are+(addition),-(subtraction),*(multiplication),/(division), and%(modulo). The following table summarizes the binary arithmetic operators in the Java programming language.
Binary Arithmetic Operators Operator Use Description +op1 + op2Adds op1andop2; also used to concatenate strings-op1 - op2Subtracts op2fromop1*op1 * op2Multiplies op1byop2/op1 / op2Divides op1byop2%op1 % op2Computes the remainder of dividing op1byop2The following example program,
ArithmeticDemo, defines two integers and two double-precision floating-point numbers and uses the five arithmetic operators to perform different arithmetic operations. This program also uses
+to concatenate strings. The arithmetic operations are shown in boldface.This is the output from the program.public class ArithmeticDemo { public static void main(String[] args) { //a few numbers int i = 37; int j = 42; double x = 27.475; double y = 7.22; System.out.println("Variable values..."); System.out.println(" i = " + i); System.out.println(" j = " + j); System.out.println(" x = " + x); System.out.println(" y = " + y); //adding numbers System.out.println("Adding..."); System.out.println(" i + j = " + (i + j)); System.out.println(" x + y = " + (x + y)); //subtracting numbers System.out.println("Subtracting..."); System.out.println(" i - j = " + (i - j)); System.out.println(" x - y = " + (x - y)); //multiplying numbers System.out.println("Multiplying..."); System.out.println(" i * j = " + (i * j)); System.out.println(" x * y = " + (x * y)); //dividing numbers System.out.println("Dividing..."); System.out.println(" i / j = " + (i / j)); System.out.println(" x / y = " + (x / y)); //computing the remainder resulting //from dividing numbers System.out.println("Computing the remainder..."); System.out.println(" i % j = " + (i % j)); System.out.println(" x % y = " + (x % y)); //mixing types System.out.println("Mixing types..."); System.out.println(" j + y = " + (j + y)); System.out.println(" i * x = " + (i * x)); } }Note that when an integer and a floating-point number are used as operands to a single arithmetic operation, the result is floating point. The integer is implicitly converted to a floating-point number before the operation takes place. The following table summarizes the data type returned by the arithmetic operators based on the data type of the operands. The necessary conversions take place before the operation is performed.Variable values... i = 37 j = 42 x = 27.475 y = 7.22 Adding... i + j = 79 x + y = 34.695 Subtracting... i - j = -5 x - y = 20.255 Multiplying... i * j = 1554 x * y = 198.37 Dividing... i / j = 0 x / y = 3.8054 Computing the remainder... i % j = 37 x % y = 5.815 Mixing types... j + y = 49.22 i * x = 1016.58
Result Types of Arithmetic Operations Data Type of Result Data Type of Operands longNeither operand is a floator adouble(integer arithmetic); at least one operand is along.intNeither operand is a floator adouble(integer arithmetic); neither operand is along.doubleAt least one operand is a double.floatAt least one operand is a float; neither operand is adouble.In addition to the binary forms of
+and-, each of these operators has a unary version, as shown in the next table.
Unary Arithmetic Operators Operator Use Description ++opPromotes optointif it's abyte,short, orchar--opArithmetically negates opTwo shortcut arithmetic operators are
++, which increments its operand by 1, and--, which decrements its operand by 1. Either++or--can appear before (prefix) or after (postfix) its operand. The prefix version,++op/--op, evaluates to the value of the operand after the increment/decrement operation. The postfix version,op++/op--, evaluates to the value of the operand before the increment/decrement operation.The following program, called
SortDemo, uses
++twice and--once.This program puts 10 integer values into an array a fixed-length structure that can hold multiple values of the same type then sorts them. The boldface line of code declares an array referred to bypublic class SortDemo { public static void main(String[] args) { int[] arrayOfInts = { 32, 87, 3, 589, 12, 1076, 2000, 8, 622, 127 }; for (int i = arrayOfInts.length; --i >= 0;) { for (int j = 0; j < i; j++) { if (arrayOfInts[j] > arrayOfInts[j+1]) { int temp = arrayOfInts[j]; arrayOfInts[j] = arrayOfInts[j+1]; arrayOfInts[j+1] = temp; } } } for (int i = 0; i < arrayOfInts.length; i++) { System.out.print(arrayOfInts[i] + " "); } System.out.println(); } }arrayOfInts, creates the array, and puts 10 integer values into it. The program usesarrayOfInts.lengthto get the number of elements in the array. Individual elements are accessed with this notation:arrayOfInts[index], whereindexis an integer indicating the position of the element within the array. Note that indices begin at 0. You'll find more details and examples for arrays in the Arrayssection.
The output from this program is the following list of numbers sorted from lowest to highest.
Let's look at how the3 8 12 32 87 127 589 622 1076 2000SortDemoprogram uses--to help control the outer loop of its two nested sorting loops; here's the statement that controls that loop.Thefor (int i = arrayOfInts.length; --i >= 0;) { ... }forstatement is a looping construct that is described later in this chapter. What's important here is the code in boldface, which continues the
forloop as long as the value returned by--iis greater than or equal to zero. Using the prefix version of--means that the last iteration of this loop occurs wheniis equal to 0. If we change the code to use the postfix version of--, the last iteration of this loop occurs wheniis equal to-1, which is incorrect for this program becauseiis used as an array index and-1is not a valid array index.The other two program loops use the postfix version of
++. In both cases, the version used doesn't really matter because the value returned by the operator isn't used for anything. When the return value of one of the shortcut operations isn't used for anything, convention prefers the postfix version.The shortcut increment/decrement operators are summarized in the following table.
Shortcut Arithmetic Operators Operator Use Description ++op++Increments opby 1; evaluates to the value ofopbefore it was incremented++++opIncrements opby 1; evaluates to the value ofopafter it was incremented--op--Decrements opby 1; evaluates to the value ofopbefore it was decremented----opDecrements opby 1; evaluates to the value ofopafter it was decremented
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.