Java Reference
In-Depth Information
This has the same effect as the statement:
count = count + 5;
Of course, the expression to the right of the op= operator can be anything that is legal in the context, so
the statement:
result /= a % b/(a + b);
is equivalent to:
result = result/(a % b/(a + b));
You should note that if the type of the result of the rhs expression is different from the
type of lhs, the compiler will automatically insert a cast. In the last example, the
statement would work with result being of type int and a and b being of type
double , for instance. This is quite different from the way the normal assignment
operation is treated. A statement using the op= operator is really equivalent to: lhs =
(type _ of _ lhs)(lhs op (rhs)); Of course, this can result in information being
lost due to the cast, and you will get no indication that it has occurred.
The complete set of op= operators appears in the precedence table later in this chapter.
Mathematical Functions and Constants
Sooner or later you are likely to need mathematical functions in your programs, even if it's only
obtaining an absolute value or calculating a square root. Java provides a range of methods that support
such functions as part of the standard library stored in the package java.lang , and all these are
available in your program automatically.
The methods that support various additional mathematical functions are implemented in the class Math
as static methods, so to reference a particular function you can just write Math and a period in front
of the name of the method you wish to use. For example, to use sqrt() , which calculates the square
root of whatever you place between the parentheses, you would write Math.sqrt(aNumber) to
produce the square root of the floating point value in the variable aNumber .
The class Math includes a range of methods for standard trigonometric functions. These are:
Method
Function
Argument Type
Result Type
sin(arg)
double in
radians
double
sine of the argument
cos(arg)
cosine of the
argument
double in
radians
double
Search WWH ::




Custom Search