这篇Java教程基于JDK1.8。教程中的示例和实践不会使用未来发行版中的优化建议。
算术运算
Java语言通过算术运算符 +、-、*、/ 和 % 来支持基本的算术运算。java.lang 包中的 Math 类提供了方法和常量来完成更高级的数学运算。
Math 类中的所有方法都是static 的,因此你可以使用类直接调用它们:
Math.cos(angle);
注意: 通过使用 static import 语言特性,不需要在数学函数前加上Math 类:
import static java.lang.Math.*;
这样允许你使用简单方法名来访问Math 类的方法:
cos(angle);
常量和基本方法
Math 类有两个常量:
- Math.E 表示自然对数的底
- Math.PI 圆周率
Math 类拥有超过 40个静态方法。下表展示了一些基本方法:
方法 | 描述 |
---|---|
double abs(double d) float abs(float f) int abs(int i) long abs(long lng) | 返回参数的绝对值 |
double ceil(double d) | 返回比参数值大或相等的最小整数值,以double类型返回 |
double floor(double d) | 返回比参数值小或相等的最大整数值,以double类型返回 |
double rint(double d) | 返回与参数值最接近的整数值,以double类型返回 |
long round(double d) int round(float f) | 返回与参数值最接近的long或者int,具体返回类型以方法返回值类型为准 |
double min(double arg1, double arg2) float min(float arg1, float arg2) int min(int arg1, int arg2) long min(long arg1, long arg2) | 返回两个参数中最小的那个 |
double max(double arg1, double arg2) float max(float arg1, float arg2) int max(int arg1, int arg2) long max(long arg1, long arg2) | 返回两个参数中最大的那个 |
下面的程序描述了如何使用这些方法:
public class BasicMathDemo {
public static void main(String[] args) {
double a = -191.635;
double b = 43.74;
int c = 16, d = 45;
System.out.printf("The absolute value " + "of %.3f is %.3f%n",
a, Math.abs(a));
System.out.printf("The ceiling of " + "%.2f is %.0f%n",
b, Math.ceil(b));
System.out.printf("The floor of " + "%.2f is %.0f%n",
b, Math.floor(b));
System.out.printf("The rint of %.2f " + "is %.0f%n",
b, Math.rint(b));
System.out.printf("The max of %d and " + "%d is %d%n",
c, d, Math.max(c, d));
System.out.printf("The min of of %d " + "and %d is %d%n",
c, d, Math.min(c, d));
}
}
输出为:
The absolute value of -191.635 is 191.635
The ceiling of 43.74 is 44
The floor of 43.74 is 43
The rint of 43.74 is 44
The max of 16 and 45 is 45
The min of 16 and 45 is 16
指数方法和对数方法
下表列出了Math 类中的指数和对数方法:
方法 | 描述 |
---|---|
double exp(double d) | 返回自然对数e的幂次方 |
double log(double d) | 返回参数值的自然对数2 |
double pow(double base, double exponent) | 返回第一个参数的值的次幂 |
double sqrt(double d) | 返回参数的平方根 |
下面示例打印e的值,之后依次调用了上表列出的方法:
public class ExponentialDemo {
public static void main(String[] args) {
double x = 11.635;
double y = 2.76;
System.out.printf("The value of " + "e is %.4f%n",
Math.E);
System.out.printf("exp(%.3f) " + "is %.3f%n",
x, Math.exp(x));
System.out.printf("log(%.3f) is " + "%.3f%n",
x, Math.log(x));
System.out.printf("pow(%.3f, %.3f) " + "is %.3f%n",
x, y, Math.pow(x, y));
System.out.printf("sqrt(%.3f) is " + "%.3f%n",
x, Math.sqrt(x));
}
}
输出如下:
The value of e is 2.7183
exp(11.635) is 112983.831
log(11.635) is 2.454
pow(11.635, 2.760) is 874.008
sqrt(11.635) is 3.411
三角函数方法
Math 类还提供了一组三角函数,在下表中进行了总结。传入每个方法的值都是用弧度表示的角度。可以使用toRadians方法将角度转换为弧度。
方法 | 描述 |
---|---|
double sin(double d) | 返回指定参数的正弦值 |
double cos(double d) | 返回指定参数的余弦值 |
double tan(double d) | 返回指定参数的正切线 |
double asin(double d) | 返回指定参数的反正弦值 |
double acos(double d) | 返回指定参数的反余弦值 |
double atan(double d) | 返回指定参数的反正切线 |
double atan2(double y, double x) | 将直角坐标(x, y)转换为极坐标(r,)并返回 |
double toDegrees(double d) double toRadians(double d) | 将参数转换为度或弧度 |
下面的示例使用这里面的每个方法来运算45度角的三角函数值:
public class TrigonometricDemo {
public static void main(String[] args) {
double degrees = 45.0;
double radians = Math.toRadians(degrees);
System.out.format("The value of pi " + "is %.4f%n",
Math.PI);
System.out.format("The sine of %.1f " + "degrees is %.4f%n",
degrees, Math.sin(radians));
System.out.format("The cosine of %.1f " + "degrees is %.4f%n",
degrees, Math.cos(radians));
System.out.format("The tangent of %.1f " + "degrees is %.4f%n",
degrees, Math.tan(radians));
System.out.format("The arcsine of %.4f " + "is %.4f degrees %n",
Math.sin(radians),
Math.toDegrees(Math.asin(Math.sin(radians))));
System.out.format("The arccosine of %.4f " + "is %.4f degrees %n",
Math.cos(radians),
Math.toDegrees(Math.acos(Math.cos(radians))));
System.out.format("The arctangent of %.4f " + "is %.4f degrees %n",
Math.tan(radians),
Math.toDegrees(Math.atan(Math.tan(radians))));
}
}
输出如下:
The value of pi is 3.1416
The sine of 45.0 degrees is 0.7071
The cosine of 45.0 degrees is 0.7071
The tangent of 45.0 degrees is 1.0000
The arcsine of 0.7071 is 45.0000 degrees
The arccosine of 0.7071 is 45.0000 degrees
The arctangent of 1.0000 is 45.0000 degrees
随机值
random()方法返回一个介于0.0和1.0之间的伪随机选择的数字。范围包括0.0但不包括1.0。换句话说:0.0 <= Math.random() < 1.0。为获取不在范围内的数字,可以在随机函数返回值之上完成算术运算。比如:为生成范围在0~9内的整数,你可以:
int number = (int)(Math.random() * 10);
通过乘以10,值的范围变成了 0.0 <= number < 10.0。
当你需要生成单随机值使用 Math.random()方法是一个不错的选择。当你要生成一系列随机值,可以创建一个java.util.Random 对象,调用对象方法生成数值。