在Java中,数字处理和数学运算主要依赖于基本数据类型(如int
、double
)及其包装类(如Integer
、Double
),以及Math
、BigDecimal
、Random
等工具类。以下是核心类、方法及常用数学工具的概述:
1. 基本数据类型与包装类
基本类型
- 整数:
byte
、short
、int
、long
(默认int
,长整型需加L
后缀,如100L
)。 - 浮点数:
float
(需加F
后缀,如3.14F
)、double
(默认,如3.14
)。 - 布尔:
boolean
(true
/false
)。 - 字符:
char
(单引号,如'A'
)。
包装类
提供装箱(Integer.valueOf(10)
)、拆箱(Integer i = 10; int j = i;
)及类型转换方法:
int num = Integer.parseInt("123"); // 字符串转整数
double d = Double.parseDouble("3.14"); // 字符串转双精度
String str = Double.toString(3.14); // 数值转字符串
2. Math
类(静态工具类)
提供常用数学函数,无需实例化,直接通过Math.方法()
调用。
常用方法
-
基本运算:
double abs = Math.abs(-10.5); // 绝对值:10.5 double max = Math.max(5, 10); // 最大值:10 double min = Math.min(5.5, 3.2); // 最小值:3.2 double sum = Math.addExact(5, 3); // 精确加法(溢出抛异常)
-
幂、对数、开方:
double pow = Math.pow(2, 3); // 幂运算:2³ = 8.0 double sqrt = Math.sqrt(16); // 平方根:4.0 double log = Math.log(10); // 自然对数:ln(10) ≈ 2.302
-
三角函数与角度转换:
double sin = Math.sin(Math.toRadians(30)); // sin(30°) = 0.5 double cos = Math.cos(Math.PI / 2); // cos(π/2) = 0.0 double atan = Math.atan(1); // arctan(1) = π/4 ≈ 0.785
-
随机数与舍入:
double random = Math.random(); // [0, 1) 随机数 long round = Math.round(3.5); // 四舍五入:4 double ceil = Math.ceil(3.2); // 向上取整:4.0 double floor = Math.floor(3.9); // 向下取整:3.0
3. 高精度计算:BigDecimal
与BigInteger
BigDecimal
(十进制高精度小数)
用于金融计算等需要精确小数的场景,避免浮点数精度丢失:
import java.math.BigDecimal;
BigDecimal a = new BigDecimal("0.1");
BigDecimal b = new BigDecimal("0.2");
BigDecimal sum = a.add(b); // 0.3(精确)
BigDecimal mul = a.multiply(b); // 0.02
BigDecimal div = a.divide(b, 2, RoundingMode.HALF_UP); // 0.50(保留2位小数,四舍五入)
BigInteger
(任意精度整数)
处理超出long
范围的大整数:
import java.math.BigInteger;
BigInteger largeNum = BigInteger.valueOf(1000);
BigInteger result = largeNum.pow(10); // 1000¹⁰
4. 随机数生成:Random
与ThreadLocalRandom
Random
类
生成伪随机数:
import java.util.Random;
Random random = new Random();
int randInt = random.nextInt(100); // [0, 100) 随机整数
double randDouble = random.nextDouble(); // [0, 1) 随机小数
boolean randBool = random.nextBoolean(); // 随机布尔值
ThreadLocalRandom
(线程安全)
Java 7+推荐的线程安全随机数生成器,性能更高:
import java.util.concurrent.ThreadLocalRandom;
int rand = ThreadLocalRandom.current().nextInt(5, 10); // [5, 10) 随机整数
5. 数值格式化:DecimalFormat
与NumberFormat
DecimalFormat
(自定义格式)
import java.text.DecimalFormat;
DecimalFormat df = new DecimalFormat("#,###.00");
String formatted = df.format(12345.678); // "12,345.68"
NumberFormat
(国际化)
import java.text.NumberFormat;
import java.util.Locale;
NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);
String usd = currency.format(1234.56); // "$1,234.56"
NumberFormat percent = NumberFormat.getPercentInstance();
String percentStr = percent.format(0.75); // "75%"
6. 其他工具类
StrictMath
与Math
类似,但提供严格的IEEE 754浮点数规范实现。
BitSet
高效存储和操作位集合,适用于大数据量的位运算。
MathContext
与BigDecimal
配合,指定计算精度和舍入模式。
总结
需求 | 工具类/方法 | 示例 |
---|---|---|
基本数学运算 | Math.abs() 、Math.pow() |
Math.sqrt(25) → 5.0 |
随机数生成 | Random 、ThreadLocalRandom |
random.nextInt(100) |
高精度计算 | BigDecimal 、BigInteger |
new BigDecimal("0.1").add(b) |
数值格式化 | DecimalFormat 、NumberFormat |
df.format(1234.56) → "1,234.56" |
三角函数与对数 | Math.sin() 、Math.log() |
Math.sin(Math.toRadians(90)) → 1.0 |
合理使用这些类和方法,可以高效处理Java中的各种数值计算需求。