一、Math 类
- Math 类包含执行基本数字运算的方法,如基本指数,对数,平方根和三角函数等,且Math 类中全部为静态方法。
- Math 类的常见方法:
public class MathMethod {
public static void main(String[] args) {
1.abs(): 绝对值
int abs = Math.abs(-9);
System.out.println(abs);// 9
2.pow(): 求幂
double pow = Math.pow(2, 4);// 2的4次方
System.out.println(pow);// 16
3.ceil(): 向上取整,返回>=该参数的最小整数(转成double);
double ceil = Math.ceil(3.9);
System.out.println(ceil);// 4.0
4.floor(): 向下取整,返回>=该参数的最大整数(转成double)
double floor = Math.floor(4.001);
System.out.println(floor);// 4.0
5.round(): 四舍五入
long round = Math.round(5.51);
System.out.println(round);// 6
6.sqrt(): 求开方,不能求负数
double sqrt = Math.sqrt(9.0);
System.out.println(sqrt);// 3.0
7.random(): 求随机数,返回的是 0 <= x < 1 之间的一个随机小数
/*
思考:请写出获取 a-b之间的一个随机整数,a,b均为整数 ,比如 a = 2, b=7
即返回一个数 x, 2 <= x <= 7
解读: Math.random() * (b-a) 返回的就是 0 <= 数 <= b-a
(1) (int)(a) <= x <= (int)(a + Math.random() * (b-a +1) )
(2) 使用具体的数给小伙伴介绍 a = 2 b = 7
(int)(a + Math.random() * (b-a +1) ) = (int)( 2 + Math.random()*6)
Math.random()*6 返回的是 0 <= x < 6 小数
2 + Math.random()*6 返回的就是 2<= x < 8 小数
(int)(2 + Math.random()*6) = 2 <= x <= 7
(3) 公式就是 (int)(a + Math.random() * (b-a +1) )
*/
for(int i = 0; i < 100; i++) {
System.out.println((int)(2 + Math.random() * (7 - 2 + 1)));
}
8.max() , min(): 返回最大值和最小值
int min = Math.min(1, 9);
int max = Math.max(45, 90);
System.out.println("min=" + min);// 1
System.out.println("max=" + max);// 90
}
}
二、BigInteger 和 BigDecimal 类
- BigInteger 类适合保存比较大的整型数据;BigDecimal 类适合保存精度更高的浮点型数据。
1. BigInteger 类
-
在编程中,需要处理很大的整数时,可能 long 数据类型还不能满足数据的需求,便可以使用 BigInteger 类来解决。
-
BigInteger 类中提供了 public BigInteger(String val) 构造方法,可以将BigInteger的十进制字符串表示形式转换为BigInteger。 字符串表示由一个可选的减号,后跟一个或多个十进制数字的序列组成。
-
代码演示:
public class BigInteger_ {
public static void main(String[] args) {
long l = 23788888899999999999999999999l;
System.out.println("l=" + l);// 会损失精度;
BigInteger bigInteger = new BigInteger("23788888899999999999999999999");// 传入的是一个字符串类型的十进制数据。
System.out.println(bigInteger);
}
}
- BigInteger 类常用方法:
public class BigInteger_ {
public static void main(String[] args) {
BigInteger bigInteger = new BigInteger("23788888899999999999999999999");
BigInteger bigInteger2 = new BigInteger("100999999999999999999");
// 解读
// 1. 在对 BigInteger 类对象进行加减乘除的时候,需要使用对应的方法,不能直接进行 + - * / 操作;
//2. 需要创建两个要操作的 BigInteger 类对象然后进行相应操作;
// 加法
BigInteger add = bigInteger.add(bigInteger2);
System.out.println(add);
// 减法
BigInteger subtract = bigInteger.subtract(bigInteger2);
System.out.println(subtract);
// 乘法
BigInteger multiply = bigInteger.multiply(bigInteger2);
System.out.println(multiply);
// 除法
BigInteger divide = bigInteger.divide(bigInteger2);
System.out.println(divide);
}
}
2. BigDecimal 类
-
在编程中,需要保存一个精度很高的浮点型数据时,可能 double 数据类型还不能满足数据的需求,便可以使用 BigDecimal 类来解决。
-
BigDecimal 类中提供了 public BigDecimal(String val) 构造方法,可以将BigDecimal的字符串表示BigDecimal转换为BigDecimal 。 字符串表示由可选符号 ‘+’ 或 ‘-’ 组成,后面是零个或多个十进制数字(“整数”)的序列。
-
代码演示:
public class BigDecimal_ {
public static void main(String[] args) {
double d = 1999.11111111111999999999999977788d;
System.out.println(d);// 精度不够,会损失精度
BigDecimal bigDecimal = new BigDecimal("1999.11111111111999999999999977788");
System.out.println(bigDecimal);// 不会损失精度
}
}
- BigInteger 类常用方法:
public class BigDecimal_ {
public static void main(String[] args) {
BigDecimal bigDecimal = new BigDecimal("1999.11");
BigDecimal bigDecimal2 = new BigDecimal("3");
// 解读
// 1. 如果对 BigDecimal 类对象进行运算,比如加减乘除,需要使用对应的方法
// 2. 需要创建两个 BigDecimal 类对象然后调用相应的方法即可;
// 加法
System.out.println(bigDecimal.add(bigDecimal2));
// 减法
System.out.println(bigDecimal.subtract(bigDecimal2));
// 乘法
System.out.println(bigDecimal.multiply(bigDecimal2));
// 除法 (需要注意!)
System.out.println(bigDecimal.divide(bigDecimal2));
// 可能会出现无限循环小数而抛出异常ArithmeticException;
// 解决办法:在调用divide 方法时,指定精度;
// 传入 BigDecimal.ROUND_CEILING 参数;
// 如果有无限循环小数,就会保留 分子 的精度;
System.out.println(bigDecimal.divide(bigDecimal2, BigDecimal.ROUND_CEILING));// 不会抛出异常
}
}
三、System 类
- System 类常用方法:
public class System_ {
public static void main(String[] args) {
1.exit()方法: 退出当前程序
System.out.println("ok1");
// 解读
// 1. exit(0) 表示程序退出
// 2. 0 表示一个状态 , 正常的状态
System.exit(0); // 程序退出
System.out.println("ok2");
2.arraycopy(Object src, int srcPos, Object dest, int destPos, int length):复制数组元素,比较适合底层调用;
/* 解读
主要是搞清楚这五个参数的含义
src: 源数组
srcPos: 从源数组的哪个索引位置开始拷贝
dest : 目标数组,即把源数组的数据拷贝到哪个数组
destPos: 把源数组的数据拷贝到 目标数组的哪个索引
length: 从源数组拷贝多少个数据到目标数组
*/
// 一般使用Arrays.copyOf() 完成复制数组;
int[] src={
1, 2, 3};
int[] dest = new int[3];// dest 当前是 {0,0,0}
System.arraycopy(src, 0, dest, 0, src.length);
System.out.println("dest=" + Arrays.toString(dest));// [1, 2, 3]
3.currentTimeMillens() : 返回当前时间距离1970-1-1 的毫秒数;
System.out.println(System.currentTimeMillis());
}
}
总结
- 本文是小白博主在学习B站韩顺平老师的Java网课时整理总结的学习笔记,在这里感谢韩顺平老师的网课,如有有兴趣的小伙伴也可以去看看。
- 本文详细解释了 Math类、大数类和 System 类的概念与这几个类中常用的方法,还举了很多例子讲解,这几个类中有很多方法,在使用的过程中可以通过查找API 文档来使用它们,希望小伙伴们看后能有所收获!
- 最后,如果本文有什么错漏的地方,欢迎大家批评指正!一起加油!!我们下一篇博文见吧!