一.Math
package API;public class math {public static void main(String[] main){
// 1.取绝对值System.out.println(Math.abs(-2147483648));//因为int类型为-2147482648~2147483647
// System.out.println(Math.absExact(-2147483648));使用这个显示报错
// 2.向上取整System.out.println(Math.ceil(12.3));//13.0System.out.println(Math.ceil(12.6));//13.0System.out.println(Math.ceil(-12.3));//-12.0System.out.println(Math.ceil(-12.6));//-12.0
// 3.向下取整System.out.println(Math.floor(12.3));//12.0System.out.println(Math.floor(12.6));//12.0System.out.println(Math.floor(-12.3));//-13.0System.out.println(Math.floor(-12.6));//-13.0
// 4.四舍五入System.out.println(Math.round(12.3));//12System.out.println(Math.round(12.6));//13System.out.println(Math.round(-12.3));//-12System.out.println(Math.round(-12.6));//-13
// 5.求最大值System.out.println(Math.max(12,13));//13System.out.println(Math.max(-12,-13));//-12
// 6.求最小值System.out.println(Math.min(12,13));//12System.out.println(Math.min(-12,-13));//-13
// 7.求平方根System.out.println(Math.sqrt(9));//3.0System.out.println(Math.sqrt(8));//2.8284271247461903
// 8.求立方根System.out.println(Math.cbrt(27));//3.0System.out.println(Math.cbrt(8));//2.0
// 9.求随机数System.out.println(Math.random());//0.0~1.0之间的随机数
// 10.求指数System.out.println(Math.pow(2,3));//8.0System.out.println(Math.pow(2,-3));//0.125}
}
二.System
package API;public class system {public static void main(String[] args) {/** System.exit(0)虚拟接正常停止* System.exit(1)虚拟接异常停止** */long start = System.currentTimeMillis();System.out.println(start);//当前时间毫秒值
// System.arraycopy();int[] arr1 = {1,2,3,4,5,6,7,8,9,10};int[] arr2 = new int[10];/** 参数一:源数组* 参数二:源数组起始索引* 参数三:目标数组* 参数四:目标数组起始索引* 参数五:复制长度* 1.源数组和目标数组必须是同类型* 2.数据源数组与目标数组如果都是引用数据类型,那么子类可以复制到父类,但是父类不能复制到子类,例如:Student,Person** */System.arraycopy(arr1,0,arr2,0,arr1.length);for (int i = 0; i < arr2.length; i++) {System.out.println(arr2[i]);}}
}
三.Runtime
package API;import java.io.IOException;public class runtime {public static void main(String[] args) throws IOException {// 1.获取runtime的对象Runtime r1 = Runtime.getRuntime();Runtime r2 = Runtime.getRuntime();
// System.out.println(r1==r2);只能调用一个虚拟环境
// 2.停止虚拟机
// Runtime.getRuntime().exit(0);
// 3.获取cpu线程数量System.out.println(r1.availableProcessors());
// 4.JVM从系统中获取最大内存(单位为byte,需要除以1024获取其他)System.out.println(r1.maxMemory()/1024/1024/1024+"GB");
// 5.JVM剩余内存大小System.out.println(r1.freeMemory()/1024/1024+"MB");
// 6.JVM从系统中获取的总内存大小System.out.println(r1.totalMemory()/1024/1024+"MB");
// 运行cmd命令
// 指定关机时间
// shutdown -s :默认在1分钟之后关机
// shutdown -s -t 0 立即关机
// shutdown -r -t 0 立即重启
// shutdown -a 取消关机
// Runtime.getRuntime().exec("notepad");Runtime.getRuntime().exec("shutdown -s -t 3600");Runtime.getRuntime().exec("shutdown -a");}
}
四.Object
package API;import java.util.Objects;/*
* 如果一个接口里面没有抽象方法表示当前接口为一个标记
* cloneable实现后,当前类可被克隆
* 如果没有实现,则不可克隆
* */
public class Student implements Cloneable{String name;int age;int[] data = null;public Student(String name, int age,int[] data) {this.name = name;this.age = age;this.data = data;}@Overridepublic boolean equals(Object o) {if (this == o) return true;//判断地址是否相同if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;return age == student.age && Objects.equals(name, student.name);}@Overridepublic String toString() {return "student{" +"name='" + name + '\'' +", age=" + age +",data=" + data +'}';}
// @Override
// public Object clone() throws CloneNotSupportedException {
// return super.clone();
// }@Overridepublic Object clone() throws CloneNotSupportedException {int[] data = this.data;int[] newdata = new int[data.length];for (int i = 0; i < data.length; i++) {newdata[i] = data[i];}Student student = (Student) super.clone();//调用浅复制student.data = newdata;return student;}
}
package API;import java.util.StringJoiner;public class object {public static void main(String[] args) {/** 1.public String toString()返回对象的字符串表示形式* 2.public boolean equals(Object obj)指示其他某个对象是否与此对象“相等”* 3.public Object clone()创建并返回此对象的一个副本* */
// 1.toString方法Student student1 = new Student("张三",18,null);System.out.println(student1);//底层默认调用toString方法
// 2.equals方法Student student2 = new Student("张三",18,null);System.out.println(student1.equals(student2));
// 因为重写后,比较的是属性值,所以为true,如果没有重写则表示的是object中的默写地址值
// testString s = "abc";StringBuilder sb = new StringBuilder("abc");System.out.println(s.equals(sb));//false
/*
* 因为是s调用的equals,所以要看String中的equals
* 字符串中的equals方法,先判断参数是否为字符串
* 如果是字符串,在比较字符串中的内容
* 如果不是字符串,则直接返回false
* 因为StringBulider是容器不是String
* */System.out.println(sb.equals(s));//false
/*
* 因为是sb调用的equals,所以要看StringBuilder中的equals
* StringBuilder中的equals方法,因为其中没有重写equals所以直接继承父类的方法
* 使用==比较地址值
* */}
}
package API;import java.util.Objects;public class objects {public static void main(String[] args) throws CloneNotSupportedException {/** public static boolean equals(Object a, Object b)* 先做非空判断,再比较两个对象是否相等* public static boolean isNull(Object obj)* 判断对象是否为null* 是:返回true* 否:返回false* public static boolean nonNull(Object obj)* 判断对象是否不为null* 是:返回true* 否:返回false* */Student student1 = new Student("张三",18,new int[]{1,2,3,4,5});Student student2 = new Student("张三",18,new int[]{1,2,3,4,5});System.out.println(Objects.equals(student2,student1));}
}
五.克隆
package API;public class copy {public static void main(String[] args) throws CloneNotSupportedException {/** 浅拷贝:创建一个新对象,新对象的属性和原对象相同,* 属性值是基本数据类型,则直接复制,如果属性值是引用数据类型,则复制引用地址* 所以当一个对象改变了地址值所存储的数据时,另一个对象也会改变* 深拷贝:创建一个新对象,新对象的属性和原对象相同,* 属性值是基本数据类型,则直接复制,如果属性值是引用数据类型,则重新开辟第一个地址,String复用串池* 所以当一个对象改变了地址值所存储的数据时,另一个对象不会改变* */int[] data = {1,2,3,4,5};Student student1 = new Student("张三",18,data);Student student2 = (Student) student1.clone();//强转data[0] = 100;System.out.println(student1);System.out.println(student2);}
}
六.BIGInteger
package API;import java.math.BigInteger;
import java.util.Random;public class biginteger {public static void main(String[] args) {/** 1.public Biginteger(int num,Random rnd) 随机获取0-2的num-1次方的整数* 2.public Biginteger(String val) 将字符串转换为大整数* 3.public Biginteger (Sting val, int radix) 获取指定进制的大整数* 4.public static BigInteger valueOf(long val) 将long类型的整数转换为大整数* 对象一旦创建里面的数据就不能被改变* */
// 1.随机获取0-2的num-1次方的整数BigInteger bt1 = new BigInteger(4,new Random());System.out.println(bt1);
// 2.将字符串转换为大整数:只能是整数,acb,1.2这些都会报错BigInteger bt2 = new BigInteger("1111111111111");System.out.println(bt2);
// 3.将大整数转变为指定进制的十进制BigInteger bt3 = new BigInteger("100",2);System.out.println(bt3);
// 4.将大整数转换为long类型的整数:Long最大值:9223372036854775807(Long.MAX_VALUE)
// 1.该静态方法表示的范围较小,只能在long类型的范围内,超出既报错
// 2.对常用数字-16~16之间的数字,做了优化
// 3.如果是-16~16之间的数字,则直接从数组中获取,不会创建新的对象BigInteger bt4 = BigInteger.valueOf(9223372036854775807L);System.out.println(bt4);/** public BigInteger add(BigInteger val) 加法* public BigInteger subtract(BigInteger val) 减法* public BigInteger multiply(BigInteger val) 乘法* public BigInteger divide(BigInteger val) 除法* public BigInteger remainder(BigInteger val) 取余* public BigInteger pow(int exponent) 次方* public BigInteger[] divideAndRemainder(BigInteger val) 商数和余数* public BigInteger gcd(BigInteger val) 最大公约数* public BigInteger abs() 绝对值* public BigInteger negate() 相反数* public int compareTo(BigInteger val) 比较大小* public int intValue() 转换为int类型* public long longValue() 转换为long类型* public float floatValue() 转换为float类型* public double doubleValue() 转换为double类型* */BigInteger bt5 = BigInteger.valueOf(10);BigInteger bt6 = BigInteger.valueOf(5);BigInteger bt7 = bt5.add(bt6);System.out.println(bt7);BigInteger[] bt8 = bt5.divideAndRemainder(bt6);System.out.println(bt8[0]);//商System.out.println(bt8[1]);//余数BigInteger bt9 = new BigInteger("1000");int a = bt9.intValue();System.out.println(a);}
}
七.BigDecima
package API;
import java.math.BigDecimal;
import java.math.RoundingMode;public class bigdecimal {public static void main(String[] args) {/** 1.public BigDecimal(double val) 将double类型的小数转换为大小数,会出现不精确的情况* 2.public BigDecimal(String val) 将字符串转换为大小数* 3.public static BigDecimal valueOf(double val) 将double类型的小数转换为大小数* *//** 1.如果要表示的数字大于了double的范围,则使用构造方法* 2.如果要表示的数字小于了double的范围,则使用静态方法(valueOf)方法* 3.如果是使用0~10之间的整数,则会从数组中获取,不会创建新的对象,如果是小数不管如何都是要创建对象的** */BigDecimal bd1 = new BigDecimal(1.2);System.out.println(bd1);BigDecimal bd2 = new BigDecimal("1.2");System.out.println(bd2);BigDecimal bd4 = new BigDecimal("1.3");System.out.println(bd4.add(bd2));BigDecimal bd3 = BigDecimal.valueOf(1.2);System.out.println(bd3);/** 1.加法:add* 2.减法:subtract* 3.乘法:multiply* 4.除法:divide(除数,保留的小数位数,舍入模式)* */BigDecimal bd5 = new BigDecimal("10.0");BigDecimal bd6 = new BigDecimal("2.0");System.out.println(bd5.divide(bd6));//整除System.out.println(bd5.divide(bd6,2, RoundingMode.HALF_UP));//不能整除,保留两位小数,四舍五入}
}
八.Arrays
import java.util.Arrays;
import java.util.Comparator;public class ArrayDemo {public static void main(String[] args) {// 创建一个示例数组int[] arr = {1,2,3,5,6,7};// 1. toString方法 - 将数组转换为字符串System.out.println("1. toString方法:");String arrStr = Arrays.toString(arr);System.out.println("数组内容: " + arrStr);System.out.println();// 2. binarySearch方法 - 二分查找System.out.println("2. binarySearch方法:");int index1 = Arrays.binarySearch(arr, 3);int index2 = Arrays.binarySearch(arr, 8);System.out.println("元素3在数组中的索引: " + index1);System.out.println("元素8在数组中的索引: " + index2);System.out.println();// 3. copyOf方法 - 拷贝数组System.out.println("3. copyOf方法:");int[] copiedArr = Arrays.copyOf(arr, arr.length);System.out.println("原数组: " + Arrays.toString(arr));System.out.println("拷贝数组: " + Arrays.toString(copiedArr));// 扩展数组长度int[] extendedArr = Arrays.copyOf(arr, 10);System.out.println("扩展后的数组: " + Arrays.toString(extendedArr));System.out.println();// 4. copyOfRange方法 - 拷贝数组指定范围System.out.println("4. copyOfRange方法:");int[] rangeArr = Arrays.copyOfRange(arr, 1, 4); // 索引1到3(不包括4)System.out.println("原数组: " + Arrays.toString(arr));System.out.println("拷贝范围[1,4)的数组: " + Arrays.toString(rangeArr));System.out.println();// 5. fill方法 - 填充数组System.out.println("5. fill方法:");int[] filledArr = new int[5];Arrays.fill(filledArr, 7);System.out.println("填充后的数组: " + Arrays.toString(filledArr));System.out.println();// 6. sort方法 - 默认排序System.out.println("6. sort方法(默认):");int[] sortArr = Arrays.copyOf(arr, arr.length);System.out.println("排序前: " + Arrays.toString(sortArr));Arrays.sort(sortArr);System.out.println("排序后: " + Arrays.toString(sortArr));System.out.println();// 7. sort方法 - 自定义排序规则System.out.println("7. sort方法(自定义规则):");Integer[] customSortArr = {5, 2, 8, 1, 9};System.out.println("排序前: " + Arrays.toString(customSortArr));// 使用Comparator实现降序排序Arrays.sort(customSortArr, new Comparator<Integer>() {
// 匿名内部类的方法
// Comparator<Integer> comparator = new Comparator<Integer>() {
// @Override
// public int compare(Integer o1, Integer o2) {
// return o2 - o1; // 降序排列
// }
// }@Overridepublic int compare(Integer o1, Integer o2) {return o2 - o1; // 降序排列}});System.out.println("降序排序后: " + Arrays.toString(customSortArr));// 使用Lambda表达式实现升序排序Arrays.sort(customSortArr, (a, b) -> a - b);System.out.println("升序排序后: " + Arrays.toString(customSortArr));}
}
1. toString方法:
数组内容: [1, 2, 3, 5, 6, 7]2. binarySearch方法:
元素3在数组中的索引: 2
元素8在数组中的索引: -73. copyOf方法:
原数组: [1, 2, 3, 5, 6, 7]
拷贝数组: [1, 2, 3, 5, 6, 7]
扩展后的数组: [1, 2, 3, 5, 6, 7, 0, 0, 0, 0]4. copyOfRange方法:
原数组: [1, 2, 3, 5, 6, 7]
拷贝范围[1,4)的数组: [2, 3, 5]5. fill方法:
填充后的数组: [7, 7, 7, 7, 7]6. sort方法(默认):
排序前: [1, 2, 3, 5, 6, 7]
排序后: [1, 2, 3, 5, 6, 7]7. sort方法(自定义规则):
排序前: [5, 2, 8, 1, 9]
降序排序后: [9, 8, 5, 2, 1]
升序排序后: [1, 2, 5, 8, 9]