Java Reference
In-Depth Information
Math.abs(-5) =
absolute value of
-5:
int
value
5
Math.abs(-5.0) =
absolute value of
-5.0:
double
value
5.0
Here is a partial list of functions in class
Math
:
Math.abs(a) =
absolute value of argument
a
.
Math.sqrt(a) =
square root of argument
a
.
Math.sin(a) =
sine of argument
a
.
Math.min(a, b) =
smaller of arguments
a
and
b
.
Math.max(a, b) =
larger of arguments
a
and
b
.
Math.floor(a) =
largest integer that is not larger than
a
.
For example,
Math.floor(3.9) = 3.0
.
Math.ceil(a) =
smallest integer that is not smaller than
a
.
For example,
Math.ceil(-3.9) = -3.0
.
If you want the floor of a
double
value as an
int
, then you have to cast it:
(
int
) Math.floor(3.9)
is the
int
3
.
1.1.8
Self-review exercises
These exercises will help you gain fluency with expressions in Java. Do not
restrict your experiments to these exercises. Make up your own. If you are not
sure what something does, study the material again, but also try it out. The more
practice you get, the more fluent you will be and the easier the later material will
seem to be.
SR1.
Type the following
int
expressions into your IDE and see what their val-
ues are:
5+2 5+2*5 (5+2)*5
4-3-3 4-(3-3) -4--4--4
6/2 6/3 6/4
6%2 6%3 6%4
-6%4 6%-4 -6%-4
Integer.MIN_VALUE Integer.MIN_VALUE - 1
Integer.MIN_VALUE + 1
Integer.MAX_VALUE Integer.MAX_VALUE - 1
Integer.MAX_VALUE + 1
SR2.
Evaluate the following
double
expressions:
5.0 + 2.0
1 + 1.99
(5 + 2.1) * 5
4.0-3-3
4.0-(3-3)
-4.0 - -4 - -4
6.0 / 2
6.0 / 3
6.0 / 4
6.0 % 2.1
6.0 % 3
6.1 % 4
-6.0 % 4
6 % -4.0
-6.1 % -4
Double.MIN_VALUE
Double.MIN_VALUE - 1
Search WWH ::
Custom Search