常用工具类
Math类
常见的 Math
类方法:
方法 | 描述 |
---|---|
Math.PI | 常量π |
Math.E | 常量 e |
Math.abs(a); | 取绝对值 |
Math.max(a, b); | 两数取最大值 |
Math.min(a, b); | 两数取最小值 |
Math.pow(x, y); | 计算 x^y 次方 |
Math.sqrt(a); | 计算根号 a |
Math.exp(x); | 计算 e^x 次方 |
Math.log10(a); | 计算以 10 为底的对数 |
Math.random(); | 取随机数,范围 [0, 1) |
Math.round(n) | 四舍五入 |
Math.ceil(n) | 向上取整 |
Math.floor(n) | 向下取整 |
Math.sin(a); | 正弦函数 |
Math.cos(a); | 余弦函数 |
Math.tan(a); | 正切函数 |
java
Math.abs(-5.5); //5.5
Math.max(10, 5); //10
Math.min(10, 5); //5
//计算 x^y 次方
Math.pow(2, 3); //8.0
//计算根号x
Math.sqrt(16); //4.0
//计算 e^x 次方
Math.exp(2); //7.38905609893065
//计算以10为底的对数
Math.log10(100); //2.0
//四舍五入
float num = 12.5f;
Math.round(num); //13
double num2 = 12.4;
Math.round(num2); //12
Math.ceil(12.3); // 13.0
Math.floor(12.7); // 12.0
Math.sin(Math.PI / 6);
Math.cos(Math.PI / 6);
Math.tan(Math.PI / 6);
拓展:取 [10, 50) 之间的随机数。
公式:Math.random() * (最大值 - 最小值) + 最小值
java
public static void main(String[] args) {
double x = Math.random();
double min = 10;
double max = 50;
double y = x * (max - min) + min;
//只取 y 的整数部分
int n = (int) y;
System.out.println(n);
}
拓展:四舍五入时保留小数位数
java
public static void main(String[] args) {
double num3 = 12.34567;
//乘100后四舍五入,再除100
double scale = Math.pow(10, 3);
double n = Math.round(num3 * scale) / scale;
System.out.println(n); //12.34
}
HexFormat
Java 标准库提供了 HexFormat
类帮助我们处理 byte[]
与十六进制字符串的转换。
将
byte[]
数组转换为十六进制字符串:javapublic static String bytesToHexString(byte[] src) { HexFormat hf = HexFormat.of(); return hf.formatHex(src); } public static void main(String[] args) { String str = "hello"; byte[] data = str.getBytes(); String hex = bytesToHexString(data); System.out.println(hex); //68656c6c6f }
定制转换格式,可以使用定制的
HexFormat
实例:javapublic static String bytesToHexString(byte[] src, String split, String prefix) { HexFormat hf = HexFormat .ofDelimiter(split) //分隔符 .withPrefix(prefix) //前缀 .withUpperCase(); //字母大写 return hf.formatHex(src); } public static void main(String[] args) { String str = "hello"; byte[] data = str.getBytes(); String hex2 = bytesToHexString(data, " ", "0x"); System.out.println(hex2); //0x68 0x65 0x6C 0x6C 0x6F }
十六进制字符串转换为
byte[]
数组:javapublic static byte[] hexStringToBytes(String hexString) { return HexFormat.of().parseHex(hexString); } public static void main(String[] args) { String str = "hello"; byte[] data = str.getBytes(); String hex = bytesToHexString(data); System.out.println(hex); //68656c6c6f byte[] bytes = hexStringToBytes(hex); System.out.println(Arrays.toString(bytes)); //[104, 101, 108, 108, 111] }
Random
Random用来创建 伪随机数。所谓伪随机数,是指只要给定一个初始的种子,产生的随机数序列是完全一样的。
要生成一个随机数,可以使用以下 4 个方法:
方法 | 描述 |
---|---|
random.nextInt(a); | int 类型随机数 |
random.nextLong(a); | long 类型随机数 |
random.nextFloat(a); | float 类型随机数 |
random.nextDouble(a); | double 类型随机数 |
java
public static void main(String[] args) {
Random random = new Random();
random.nextInt();
random.nextInt(10); //随机生成 0 ~ 10 之间的随机数
random.nextLong();
random.nextFloat();
random.nextDouble();
}
在创建 Random 实例时,如果不给定种子,就是用系统当前时间戳作为种子。因此每次运行时,种子不同,得到的伪随机数就不同。
如果我们再创建 Random 实例时,指定一个种子,就会得到完全确定的随机数序列:
java
public static void main(String[] args) {
Random r = new Random(123);
for (int i = 0; i < 10; i++) {
System.out.println(r.nextInt(100)); //每次产生的随机数都固定
}
}
SecureRandom
有伪随机数,就存在真随机数。SecureRandom
就是用来创建安全的随机数的。
在密码学中,安全的随机数非常重要。如果使用不安全的伪随机数,所有加密体系都将被攻破。
java
SecureRandom random = new SecureRandom();
random.nextInt(100);
SecureRandom 无法指定种子,在实际使用的时候,可以优先获取高强度的安全随机数生成器,如果没有提供,在使用普通等级的安全随机数生成器:
java
public static void main(String[] args) {
SecureRandom random = null;
try {
random = SecureRandom.getInstanceStrong();
} catch (NoSuchAlgorithmException e) {
random = new SecureRandom();
}
byte[] buffer = new byte[4];
random.nextBytes(buffer);
System.out.println(Arrays.toString(buffer)); //[-22, -114, -41, 74]
}