Java Reference
In-Depth Information
Incidentally...
Modular arithmetic finds many computer uses. One of them is in cal-
culating functions that have repeating values, called periodic func-
tions. For example, the math unit of the Pentium microprocessor
produces trigonometric functions in the range 0 to 45 degrees. Soft-
ware must then use remainder calculations to scale the functions to
any desired angle.
Concatenation
InJava,the+operator,whichisusedforarithmeticaddition,isalsousedto
concatenatestrings.Theterm“concatenation”comesfromtheLatinword
“catena,” which means chain. To concatenate strings is to chain them to-
gether. The following code fragment shows the action of this operator:
// Define strings
String str1 = “con”;
String str2 = “ca”;
String str3 = “ten”;
String str4 = “ate”;
// Form a new word using string concatenation
String result = str1 + str2 + str3 + str4;
// result = “concatenate”
The operation of the concatenation operator can be viewed as a form
of string “addition.” In Java, if a numeric value is added to a string the
number is first converted into a string of digits and then concatenated to
the string operand, as shown in the following code fragment:
String str1 = “Catch ”; // Define a string
int value = 22;
// Define an int
result = str5 + value;
// Concatenate string + int
// result = “Catch 22"
Note that concatenation requires that one of the operands be a string.
Ifbothoperandsarenumericvaluesthenarithmeticadditiontakesplace.
Increment and Decrement
Programsoftenhavetokeepcountofthenumberoftimesanoperation,or
aseriesoroperations,hastakenplace.Inordertokeepthetallycountitis
convenient to have a simple form of adding 1 to the value of a variable or
subtracting 1 from the value of a variable. Java contains simple operators
that allow this manipulation. These operators, which originated in the C
language,arecalledthe increment (++)and decrement (- -)operators.For
Search WWH ::




Custom Search