Math类
常用方法:
public class Test01 {
public static void main(String[] args) {
//获取绝对值
int abs = Math.abs(-10);
//向上取整
double ceil = Math.ceil(4.5f);
//向下取整
double floor = Math.floor(10.3);
//获取两个值中的较大值
int max = Math.max(10, 20);
//返回a的b次幂值
double pow = Math.pow(2.0, 3.0);
//四舍五入
int round = Math.round(4.6f);
//返回值为double的随机值,范围[0.0,1.0)
double random = Math.random();
//获取π值
double pi = Math.PI;
System.out.println(abs);
System.out.println(ceil);
System.out.println(floor);
System.out.println(max);
System.out.println(pow);
System.out.println(round);
System.out.println(random);
System.out.println(pi);
}
}
System类
常用方法:
public class Test01 {
public static void main(String[] args) {
//返回当前系统的时间,毫秒值形式
System.out.println(System.currentTimeMillis());
//copy数组,数据源数组跟目的地数组类型必须一致(基本数据类型)、在copy的时候需要考虑数组的长度、
//数据源数组跟目的地数组类型为引用类型时,那么子类类型可以赋值给父类类型
//参数1:数据源数组
//参数2:数据源数组起始索引,从第几个开始copy
//参数3:目的地数组
//参数4:目的地数组起始索引
//参数5:拷贝个数
int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = new int[5];
System.arraycopy(arr1, 0, arr2, 0, 3);
for (int j : arr2) {
System.out.print(j + " ");
}
//0表示当前虚拟机正常停止
//非0表示当前虚拟机异常停止
System.exit(0);
System.out.println("你看我执行吗?");
}
}
Runtime类
常用方法:
public class Test01 {
public static void main(String[] args) throws IOException {
//获取runtime对象
Runtime runtime = Runtime.getRuntime();
//获取当前操作系统的cpu线程数
int threadNum = runtime.availableProcessors();
//jvm能从系统中获取的总内存大小(byte)
long maxMemory = runtime.maxMemory();
//jvm已经从系统中获取总内存大小(byte)
long totalMemory = runtime.totalMemory();
//jvm剩余内存大小(byte)
long freeMemory = runtime.freeMemory();
System.out.println(threadNum);
System.out.println(maxMemory / 1024 / 1024);
System.out.println(totalMemory / 1024 / 1024);
System.out.println(freeMemory / 1024 / 1024);
//执行cmd命令
runtime.exec("notepad");
//退出虚拟机
runtime.exit(0);
}
}
Object类
常用方法:
public class Test01 {
public static void main(String[] args) throws IOException, CloneNotSupportedException {
//默认情况下,Object类中toString()方法返回的是地址值
Object o = new Object();
System.out.println(o.toString());
//如果没有重写equals(),那么默认使用的是Object中的方法进行比较,比较的是地址值是否相等
//一般来讲地址值对于我们意义不大,所以我们会重写,重写之后比较的就是对象内部的属性值了
Student student1 = new Student();
Student student2 = new Student();
System.out.println(student1.equals(student2));//false
//clone,把A对象的属性值完全拷贝给B对象,也叫对象拷贝,对象复制
//注意细节:
//1.重写Object里面的clone()方法
//2.让javabean类实现Cloneable接口
Student student3 = new Student(20, "lisi");
Student student4 = (Student) student3.clone();
System.out.println(student3);
System.out.println(student4);
}
}
- Object是Java中的顶级父类,所有的类都直接或间接的继承于Object类。
- toString():一般会重写,打印对象时打印属性
- equals():比较对象时会重写,比较对象属性值是否相同
- clone():默认浅克隆,如果需要深克隆需要重写方法或者使用第三方工具类。
- 浅克隆:不管对象内部的属性是基本数据类型还是引用数据类型,都完全拷贝过来
- 深克隆:基本数据拷贝过来、字符串复用、引用数据类型会重新创建新的
clone()方法注意事项:
- 重写Object中clone方法
- 让javabean实现Cloneable接口
Objects工具类
Objects是一个工具类,提供了一些方法去完成一些功能。
//先做非空判断,再比较两个对象
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
//判断对象是否为null,为null返回true,反之
public static boolean isNull(Object obj) {
return obj == null;
}
//判断对象是否为null,跟isNull的结果相反
public static boolean nonNull(Object obj) {
return obj != null;
}
BigInteger
在java中,整数有四种类型:byte、short、int、long。在底层分别占用1个字节、2个字节、4个字节、8个字节。
- 如果BigInteger表示的数字没有超出long的范围,可以用静态方法获取。
- 如果BigInteger表示的超出long的范围,可以用构造方法获取。
- 对象一旦创建,BigInteger内部记录的值不能发生改变
- 只要进行计算都会产生一个新的BigInteger对象
public class Test01 {
public static void main(String[] args) throws IOException, CloneNotSupportedException {
Random random = new Random();
//如果BigInteger表示的数字没有超出long的范围,可以用静态方法获取。
BigInteger bigInteger1 = BigInteger.valueOf(Long.MAX_VALUE);
//如果BigInteger表示的超出long的范围,可以用构造方法获取。
BigInteger bigInteger2 = new BigInteger("999999999999999999999999999999");
//对象一旦创建,BigInteger内部记录的值不能发生改变
BigInteger bigInteger3 = BigInteger.valueOf(5);
BigInteger bigInteger4 = BigInteger.valueOf(6);
//只要进行计算都会产生一个新的BigInteger对象
BigInteger bigInteger5 = bigInteger3.add(bigInteger4);
System.out.println(bigInteger3 == bigInteger5);//false
//在内部对常用的数字:-16~16进行了优化,如果多次获取不会创建新的
BigInteger bigInteger6 = BigInteger.valueOf(16);
BigInteger bigInteger7 = BigInteger.valueOf(16);
System.out.println(bigInteger6 == bigInteger7);
//获取随机大整数,范围:[0,2的num次方-1]
BigInteger bigInteger8 = new BigInteger(4, random);
System.out.println(bigInteger8);
//获取指定进制大整数,将”100“转换为2进制下的数据
BigInteger bigInteger9 = new BigInteger("100", 2);
}
}
BigDecimal类
作用:表示较大的小数和解决小数进度失真的问题。
获取BigDecimal对象
//构造方法获取BigDecimal对象
//建议使用通过传递字符串的这种方式
BigDecimal bigDecimal1 = new BigDecimal("0.9");
//这种方式不建议使用,有可能不精确
BigDecimal bigDecimal2 = new BigDecimal(0.9);
//静态方法获取BigDecimal对象
BigDecimal bigDecimal3 = BigDecimal.valueOf(0.1);
注意细节:
- 如果要表示的数字不大,没有超出double的取值范围,建议使用静态方法。
- 如果要表示的数字很大,超出了double的取值范围,建议使用构造方法。
- 如果我们传递的是[0,10]之间的整数,那么方法会返回已经创建好的对象,不会重新new
public class Test01 {
public static void main(String[] args) throws IOException, CloneNotSupportedException {
//如果我们传递的是[0,10]之间的整数,那么方法会返回已经创建好的对象,不会重新new
BigDecimal bigDecimal1 = BigDecimal.valueOf(8);
BigDecimal bigDecimal2 = BigDecimal.valueOf(8);
System.out.println(bigDecimal1 == bigDecimal2);//true
}
}
常见操作:
public class Test01 {
public static void main(String[] args) throws IOException, CloneNotSupportedException {
BigDecimal bigDecimal1 = BigDecimal.valueOf(0.9);
BigDecimal bigDecimal2 = BigDecimal.valueOf(0.6);
//加法
BigDecimal add = bigDecimal1.add(bigDecimal2);
System.out.println(add);
//减法
BigDecimal subtract = bigDecimal1.subtract(bigDecimal2);
System.out.println(subtract);
//乘法
BigDecimal multiply = bigDecimal1.multiply(bigDecimal2);
System.out.println(multiply);
//除法,参数1:BigDecimal对象、参数2:小数点保留后几位、参数3:舍入模式
BigDecimal divide = bigDecimal1.divide(bigDecimal2,2, RoundingMode.HALF_UP);
System.out.println(divide);
}
}
包装类
- 基本数据类型对应的对象
- jdk5以后新增了自动装箱、自动拆箱特性。
自动装箱:把基本数据类型会自动的变成其对应的包装类
自动拆箱:把包装类自动的变成其对象的基本数据类型
public class Test01 {
public static void main(String[] args) {
Integer i1 = new Integer(127);
Integer i2 = new Integer(127);
System.out.println(i1 == i2);//false
Integer i3 = Integer.valueOf(127);
Integer i4 = Integer.valueOf(127);
System.out.println(i3 == i4);//true
Integer i5 = Integer.valueOf("128");
Integer i6 = Integer.valueOf("128");
System.out.println(i5==i6);//false
Integer i7 = Integer.valueOf("127");
System.out.println(i3 == i7);//true
//自动装箱,自动拆箱
Integer i8 = 127;
Integer i9 = 127;
System.out.println(i8 == i9);//true
Integer i10 = i8 + i9;
System.out.println(i10);
}
}
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
8种包装类当中,除了Character都有对应的parsexxx()方法,进行类型转换
int i11 = Integer.parseInt("100", 2);
int i12 = Integer.parseInt("100", 8);
int i13 = Integer.parseInt("100", 10);
int i14 = Integer.parseInt("100", 16);