【Java】Java 常用核心类篇(二)

目录

  • Math 类
    • Math 类概述
    • 常用常量
      • Math.PI
      • Math.E
    • 常用方法
      • 三角函数方法
      • 反三角函数方法
      • 指数和对数方法
      • 取整方法
      • 绝对值和最值方法
      • 随机数方法
      • 角度转换方法

Math 类

Math 类概述

在 Java 编程里,Math 类是一个极其重要的工具类,它存于 java.lang 包下。java.lang 包是 Java 语言的核心包,在使用其中的类时,无需额外导入,Java 编译器会自动处理。
Math 类有两个显著特点:

  1. 被 final 修饰:这表明 Math 类不能被其他类继承,是最终形态,就像一个已经完美成型、不能再被修改结构的建筑。
  2. 构造方法私有:意味着无法通过 new 关键字来创建 Math 类的对象。不过别担心,Math 类中的所有属性和方法都是静态的,我们可以直接通过类名来调用,例如 Math.abs(-5)。

常用常量

Math.PI

Math.PI 代表圆周率 π,其近似值为 3.141592653589793。在处理与圆、球等几何图形相关的计算时,这个常量非常有用。

public class PiExample {
    public static void main(String[] args) {
        // 定义圆的半径
        double radius = 5.0;

        // 计算圆的周长,公式为 C = 2 * π * r
        double circumference = 2 * Math.PI * radius;
        System.out.println("半径为 " + radius + " 的圆的周长是: " + circumference);

        // 计算圆的面积,公式为 S = π * r * r
        double area = Math.PI * radius * radius;
        System.out.println("半径为 " + radius + " 的圆的面积是: " + area);
    }
}
  • 首先定义了圆的半径 radius 为 5.0。
  • 然后根据圆的周长公式 C = 2πr 计算周长,使用 Math.PI 来表示 π。
  • 接着依据圆的面积公式 S = πr2 计算面积。
  • 最后将计算结果输出。

Math.E

Math.E 表示自然对数的底数 e,近似值是 2.718281828459045。在涉及指数、对数等数学计算时会用到它。

public class EExample {
    public static void main(String[] args) {
        // 计算 e 的 3 次方
        double power = Math.pow(Math.E, 3);
        System.out.println("e 的 3 次方是: " + power);

        // 计算以 e 为底 10 的对数
        double log = Math.log(10);
        System.out.println("以 e 为底 10 的对数是: " + log);
    }
}
  • 使用 Math.pow() 方法计算 e 的 3 次方,Math.pow() 方法的第一个参数是底数,第二个参数是指数。
  • 利用 Math.log() 方法计算以 e 为底 10 的对数。

常用方法

三角函数方法

三角函数在数学和物理计算中经常出现,Math 类提供了计算正弦、余弦、正切的方法,这些方法的参数都是以弧度为单位的角度。

  • Math.sin(double a):该方法返回指定角度(以弧度为单位)的正弦值。
  • Math.cos(double a):返回指定角度(以弧度为单位)的余弦值。
  • Math.tan(double a):返回指定角度(以弧度为单位)的正切值。
public class TrigonometricExample {
    public static void main(String[] args) {
        // 将 45 度转换为弧度,因为 Math 类的三角函数方法参数是弧度制
        double angleInRadians = Math.toRadians(45);

        // 计算正弦值
        double sinValue = Math.sin(angleInRadians);
        System.out.println("45 度的正弦值是: " + sinValue);

        // 计算余弦值
        double cosValue = Math.cos(angleInRadians);
        System.out.println("45 度的余弦值是: " + cosValue);

        // 计算正切值
        double tanValue = Math.tan(angleInRadians);
        System.out.println("45 度的正切值是: " + tanValue);
    }
}
  • 先使用 Math.toRadians() 方法将 45 度转换为弧度,因为 Math 类的三角函数方法要求参数是弧度制。
  • 然后分别调用 Math.sin()、Math.cos() 和 Math.tan() 方法计算正弦、余弦和正切值,并将结果输出。

反三角函数方法

反三角函数用于根据三角函数值求对应的角度,返回值是弧度制。

  • Math.asin(double a):返回指定值的反正弦值,结果在 [- π 2 \frac{π}{2} 2π, π 2 \frac{π}{2} 2π] 范围内。
  • Math.acos(double a):返回指定值的反余弦值,结果在 [0,π] 范围内。
  • Math.atan(double a):返回指定值的反正切值,结果在 [- π 2 \frac{π}{2} 2π, π 2 \frac{π}{2} 2π] 范围内。
public class InverseTrigonometricExample {
    public static void main(String[] args) {
        // 定义一个值
        double value = 0.5;

        // 计算反正弦值
        double asinValue = Math.asin(value);
        // 将弧度转换为度
        double asinDegrees = Math.toDegrees(asinValue);
        System.out.println("0.5 的反正弦值(度)是: " + asinDegrees);

        // 计算反余弦值
        double acosValue = Math.acos(value);
        double acosDegrees = Math.toDegrees(acosValue);
        System.out.println("0.5 的反余弦值(度)是: " + acosDegrees);

        // 计算反正切值
        double atanValue = Math.atan(value);
        double atanDegrees = Math.toDegrees(atanValue);
        System.out.println("0.5 的反正切值(度)是: " + atanDegrees);
    }
}
  • 定义一个值 value 为 0.5。
  • 分别调用 Math.asin()、Math.acos() 和 Math.atan() 方法计算反正弦、反余弦和反正切值。
  • 由于返回值是弧度制,使用 Math.toDegrees() 方法将其转换为度并输出。

指数和对数方法

  • Math.exp(double a):返回 e 的指定次幂,即 ea
  • Math.log(double a):返回指定值的自然对数(以 e 为底)。
  • Math.log10(double a):返回指定值的常用对数(以 10 为底)。
  • Math.pow(double a, double b):返回第一个参数的第二个参数次幂,即 ab
  • Math.sqrt(double a):返回指定值的平方根。
public class ExponentAndLogExample {
    public static void main(String[] args) {
        // 计算 e 的 2 次方
        double expResult = Math.exp(2);
        System.out.println("e 的 2 次方是: " + expResult);

        // 计算以 e 为底 10 的对数
        double logResult = Math.log(10);
        System.out.println("以 e 为底 10 的对数是: " + logResult);

        // 计算以 10 为底 100 的对数
        double log10Result = Math.log10(100);
        System.out.println("以 10 为底 100 的对数是: " + log10Result);

        // 计算 2 的 3 次方
        double powerResult = Math.pow(2, 3);
        System.out.println("2 的 3 次方是: " + powerResult);

        // 计算 25 的平方根
        double sqrtResult = Math.sqrt(25);
        System.out.println("25 的平方根是: " + sqrtResult);
    }
}
  • 使用 Math.exp() 方法计算 e 的 2 次方。
  • 用 Math.log() 方法计算以 e 为底 10 的对数,Math.log10() 方法计算以 10 为底 100 的对数。
  • Math.pow() 方法用于计算 2 的 3 次方,Math.sqrt() 方法计算 25 的平方根。

取整方法

  • Math.ceil(double a):返回大于或等于指定数的最小整数,结果为 double 类型。可以理解为 “向上取整”,就像把一个小数 “推” 到比它大的最近的整数位置。
  • Math.floor(double a):返回小于或等于指定数的最大整数,结果为 double 类型。也就是 “向下取整”,把小数 “拉” 到比它小的最近的整数位置。
  • Math.rint(double a):返回最接近参数的整数,如果两个整数同样接近,则返回偶数。结果为 double 类型。
  • Math.round(float a):返回最接近参数的 int 类型整数,四舍五入。
  • Math.round(double a):返回最接近参数的 long 类型整数,四舍五入。
public class RoundingExample {
    public static void main(String[] args) {
        double num = 3.7;

        // 向上取整
        double ceilValue = Math.ceil(num);
        System.out.println(num + " 向上取整的结果是: " + ceilValue);

        // 向下取整
        double floorValue = Math.floor(num);
        System.out.println(num + " 向下取整的结果是: " + floorValue);

        // 返回最接近的整数(若两个同样接近则返回偶数)
        double rintValue = Math.rint(num);
        System.out.println(num + " 最接近的整数(规则处理)是: " + rintValue);

        // 四舍五入为 int 类型
        int roundInt = Math.round((float) num);
        System.out.println(num + " 四舍五入为 int 类型的结果是: " + roundInt);

        // 四舍五入为 long 类型
        long roundLong = Math.round(num);
        System.out.println(num + " 四舍五入为 long 类型的结果是: " + roundLong);
    }
}
  • 定义一个小数 num 为 3.7。
  • 分别调用 Math.ceil()、Math.floor()、Math.rint()、Math.round(float a) 和 Math.round(double a) 方法进行取整操作,并输出结果。

绝对值和最值方法

  • Math.abs(int a):返回整数的绝对值,还有 abs(long a)、abs(float a)、abs(double a) 等重载方法,分别用于处理不同数据类型的绝对值计算。
  • Math.max(int a, int b):返回两个整数中的较大值,同样有 max(long a, long b)、max(float a, float b)、max(double a, double b) 等重载形式。
  • Math.min(int a, int b):返回两个整数中的较小值,也有多种重载形式。
public class AbsMaxMinExample {
    public static void main(String[] args) {
        int num1 = -5;
        int num2 = 10;

        // 计算绝对值
        int absValue = Math.abs(num1);
        System.out.println(num1 + " 的绝对值是: " + absValue);

        // 找出较大值
        int maxValue = Math.max(num1, num2);
        System.out.println(num1 + " 和 " + num2 + " 中的较大值是: " + maxValue);

        // 找出较小值
        int minValue = Math.min(num1, num2);
        System.out.println(num1 + " 和 " + num2 + " 中的较小值是: " + minValue);
    }
}
  • 定义两个整数 num1 为 -5,num2 为 10。
  • 调用 Math.abs() 方法计算 num1 的绝对值。
  • 使用 Math.max() 方法找出 num1 和 num2 中的较大值,Math.min() 方法找出较小值,并输出结果。

随机数方法

  • Math.random():返回一个大于等于 0.0 且小于 1.0 的随机 double 类型数。可以通过一些简单的数学运算来生成指定范围内的随机数。
public class RandomExample {
    public static void main(String[] args) {
        // 生成 1 到 10 之间的随机整数
        int randomInt = (int) (Math.random() * 10 + 1);
        System.out.println("1 到 10 之间的随机整数是: " + randomInt);

        // 生成 20 到 30 之间的随机整数
        int randomInRange = (int) (Math.random() * 11 + 20);
        System.out.println("20 到 30 之间的随机整数是: " + randomInRange);
    }
}
  • 对于生成 1 到 10 之间的随机整数,Math.random() 生成的是 0.0 到 1.0 之间的随机数,乘以 10 后范围变为 0.0 到 10.0 之间,再加上 1 范围就变成 1.0 到 11.0 之间,最后通过 (int) 强制类型转换为整数,得到 1 到 10 之间的随机整数。
  • 生成 20 到 30 之间的随机整数同理,Math.random() 乘以 11 得到 0.0 到 11.0 之间的随机数,加上 20 后范围变为 20.0 到 31.0 之间,强制转换为整数后得到 20 到 30 之间的随机整数。

角度转换方法

  • Math.toRadians(double angdeg):将角度转换为弧度。因为 Math 类的三角函数方法要求参数是弧度制,所以在使用角度进行三角函数计算时,需要先将角度转换为弧度。
  • Math.toDegrees(double angrad):将弧度转换为角度。当通过反三角函数得到的结果是弧度制,而我们需要以度为单位表示时,就可以使用这个方法。
public class AngleConversionExample {
    public static void main(String[] args) {
        // 将 60 度转换为弧度
        double radians = Math.toRadians(60);
        System.out.println("60 度转换为弧度是: " + radians);

        // 将 π/3 弧度转换为度
        double degrees = Math.toDegrees(Math.PI / 3);
        System.out.println("π/3 弧度转换为度是: " + degrees);
    }
}
  • 使用 Math.toRadians() 方法将 60 度转换为弧度。
  • 利用 Math.toDegrees() 方法将 π 3 \frac{π}{3} 3π 弧度转换为度,并输出结果。

你可能感兴趣的:(#,Java,基础,java,开发语言)