Java Number & Math 类
                           
天天向上
发布: 2025-03-02 10:25:13

原创
561 人浏览过

在 Java 中,Number 类和 Math 类都是常用的类,分别处理数值和数学计算。

1. Number

Number 是 Java 中所有数字类型(ByteShortIntegerLongFloatDouble)的父类。它本身是一个抽象类,因此你无法直接创建 Number 类型的对象。它主要定义了数字转换的方法,这些方法在其子类(如 IntegerDouble 等)中都有实现。

1.1 Number 类的方法

  • byteValue(): 返回数字的 byte 类型。
  • shortValue(): 返回数字的 short 类型。
  • intValue(): 返回数字的 int 类型。
  • longValue(): 返回数字的 long 类型。
  • floatValue(): 返回数字的 float 类型。
  • doubleValue(): 返回数字的 double 类型。

这些方法允许你将一个 Number 类型的对象转换为不同的基本数据类型。

实例:

public class NumberExample {
    public static void main(String[] args) {
        Number num1 = new Integer(42);  // 创建 Integer 类型的 Number
        Number num2 = new Double(10.5); // 创建 Double 类型的 Number

        // 转换为不同的基本数据类型
        System.out.println("num1 as byte: " + num1.byteValue());   // 输出 42
        System.out.println("num1 as short: " + num1.shortValue()); // 输出 42
        System.out.println("num1 as int: " + num1.intValue());     // 输出 42
        System.out.println("num1 as long: " + num1.longValue());   // 输出 42
        System.out.println("num1 as float: " + num1.floatValue()); // 输出 42.0
        System.out.println("num1 as double: " + num1.doubleValue()); // 输出 42.0

        System.out.println("num2 as byte: " + num2.byteValue());   // 输出 10
        System.out.println("num2 as short: " + num2.shortValue()); // 输出 10
        System.out.println("num2 as int: " + num2.intValue());     // 输出 10
        System.out.println("num2 as long: " + num2.longValue());   // 输出 10
        System.out.println("num2 as float: " + num2.floatValue()); // 输出 10.5
        System.out.println("num2 as double: " + num2.doubleValue()); // 输出 10.5
    }
}

输出:

num1 as byte: 42
num1 as short: 42
num1 as int: 42
num1 as long: 42
num1 as float: 42.0
num1 as double: 42.0
num2 as byte: 10
num2 as short: 10
num2 as int: 10
num2 as long: 10
num2 as float: 10.5
num2 as double: 10.5

总结:

  • Number 类提供了将数字类型转换为其他基本类型的方法。它本身不能实例化,但可以通过子类(如 IntegerDouble)来创建对象,并调用其转换方法。

2. Math

Math 是一个包含大量常用数学函数的工具类,所有的方法都是静态的,因此你不需要创建 Math 对象就可以直接调用。

2.1 常用方法:

  • Math.abs(x): 返回 x 的绝对值。
  • Math.max(x, y): 返回 xy 中较大的一个。
  • Math.min(x, y): 返回 xy 中较小的一个。
  • Math.pow(x, y): 返回 xy 次方。
  • Math.sqrt(x): 返回 x 的平方根。
  • Math.random(): 返回一个 0.0 到 1.0 之间的随机数。
  • Math.round(x): 返回 x 四舍五入后的值。

这些方法通常用于基础的数学计算、生成随机数、以及执行数学运算。

实例:

public class MathExample {
    public static void main(String[] args) {
        // 1. Math.abs():返回绝对值
        System.out.println("Math.abs(-10): " + Math.abs(-10));  // 输出 10
        System.out.println("Math.abs(10): " + Math.abs(10));    // 输出 10

        // 2. Math.max():返回较大的值
        System.out.println("Math.max(10, 20): " + Math.max(10, 20)); // 输出 20
        System.out.println("Math.max(-5, -10): " + Math.max(-5, -10)); // 输出 -5

        // 3. Math.min():返回较小的值
        System.out.println("Math.min(10, 20): " + Math.min(10, 20)); // 输出 10
        System.out.println("Math.min(-5, -10): " + Math.min(-5, -10)); // 输出 -10

        // 4. Math.pow():计算幂
        System.out.println("Math.pow(2, 3): " + Math.pow(2, 3)); // 输出 8.0
        System.out.println("Math.pow(5, 2): " + Math.pow(5, 2)); // 输出 25.0

        // 5. Math.sqrt():计算平方根
        System.out.println("Math.sqrt(16): " + Math.sqrt(16)); // 输出 4.0
        System.out.println("Math.sqrt(25): " + Math.sqrt(25)); // 输出 5.0

        // 6. Math.random():生成随机数
        System.out.println("Math.random(): " + Math.random()); // 输出 0.0 到 1.0 之间的随机数

        // 7. Math.round():四舍五入
        System.out.println("Math.round(10.4): " + Math.round(10.4)); // 输出 10
        System.out.println("Math.round(10.6): " + Math.round(10.6)); // 输出 11
    }
}

输出:

Math.abs(-10): 10
Math.abs(10): 10
Math.max(10, 20): 20
Math.max(-5, -10): -5
Math.min(10, 20): 10
Math.min(-5, -10): -10
Math.pow(2, 3): 8.0
Math.pow(5, 2): 25.0
Math.sqrt(16): 4.0
Math.sqrt(25): 5.0
Math.random(): 0.123456789
Math.round(10.4): 10
Math.round(10.6): 11

总结:

  • Math 类是一个工具类,提供了常见的数学运算方法。你可以通过调用其静态方法进行各种数学计算。
  • 这些方法不需要创建 Math 类的实例,可以直接通过 Math 类来调用。

结合使用实例:

假设你正在开发一个应用,需要计算某个数值的平方根,并且四舍五入到最接近的整数,然后将这个结果转换为 int 类型:

public class CombinedExample {
    public static void main(String[] args) {
        Number num = new Double(25.5);

        // 1. 使用 Math.sqrt() 计算平方根
        double sqrtValue = Math.sqrt(num.doubleValue());
        System.out.println("Square root of " + num.doubleValue() + " is: " + sqrtValue);

        // 2. 使用 Math.round() 四舍五入
        long roundedValue = Math.round(sqrtValue);
        System.out.println("Rounded value: " + roundedValue);

        // 3. 转换为 int 类型
        int intValue = (int) roundedValue;
        System.out.println("Final int value: " + intValue);
    }
}

输出:

Square root of 25.5 is: 5.049752470138244
Rounded value: 5
Final int value: 5

总结:

  • Number:是所有数字类的父类,提供了将数字转换为不同类型的功能。
  • Math:提供了各种常用的数学计算方法,包括绝对值、最大值、最小值、幂运算、平方根、随机数和四舍五入等。

这两个类在日常开发中非常实用,能够简化很多数值和数学计算任务。更多详细内容,请关注其他相关文章!

发表回复 0

Your email address will not be published. Required fields are marked *