| 内容 | 作用 |
|---|---|
| E | e(即自然对数的底数)的 double 值。 |
| PI | pi(即圆的周长与直径之比)的 double 值。 |
| abs(double a) | 返回a的绝对值 |
| cbrt(double a) | 返回a的立方根 |
| exp(double a) | 返回e的a次幂 |
| log(double a) | 返回a的自然对数(底数是e) |
| log10(double a) | 返回a的对数(底数是10) |
| max(a,b) | 返回a和b中较大的一个 |
| min(a,b) | 返回a和b中较小的一个 |
| pow(a,b) | 返回a的b次幂 |
| log(double a) | 返回a的自然对数(底数是e) |
| random() | 返回double的随机数,范围是[0.0,1.0) |
| sqrt(a) | 返回a的平方根 |
| ceil(double a) | 向上取整,7.2->8 |
| floor(double a) | 向下取整,7.9->7 |
| round(double a) | 四舍五入 |
public class test53 {
public static void main(String[] args) {
//int的范围是:-2147483648 ~ 2147483647
System.out.println(Math.abs(-2147483648)); //-2147483648
System.out.println(Math.absExact(-2147483648)); //JDK15以后,会报错
}
}
public class test53 {
public static void main(String[] args) {
System.out.println(Math.ceil(13.34)); //14.0
System.out.println(Math.ceil(13.94)); //14.0
System.out.println(Math.ceil(-13.34)); //-13.0
System.out.println(Math.ceil(-13.94)); //-13.0
System.out.println(Math.floor(13.34)); //13.0
System.out.println(Math.floor(13.94)); //13.0
System.out.println(Math.floor(-13.34)); //-14.0
System.out.println(Math.floor(-13.34)); //-14.0
}
}
| 方法 | 作用 |
|---|---|
| exit() | 终止当前正在运行的 Java 虚拟机 |
| currentTimeMillis() | 返回以毫秒为单位的当前时间(1s=1000ms) |
| arraycopy() | 从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束 |
public static void exit(int status)
方法形参:状态码
public static long currentTimeMillis()
返回:当前时间与 1970 年 1 月 1 日0点之间的时间差(以毫秒为单位测量)
public static void arraycopy(数据源数据,起始索引,目的地数组,起始索引,拷贝个数)
public class test {
public static void main(String[] args) {
Student s1=new Student("cjm",22);
Student s2=new Student("cjm",22);
Student s3=new Student("cjm",22);
Student[] arr={s1,s2,s3};
Person[] parr=new Person[3];
System.arraycopy(arr,0,parr,0,3);
StringBuilder sb=new StringBuilder();
for(Person i:parr){
sb.append(i.getName()).append(" ").append(i.getAge()).append("\n");
}
System.out.println(sb);
for (int i = 0; i < parr.length; i++) {
Student stu= (Student) parr[i]; //强制转换
sb.append(stu.getName()).append(" ").append(stu.getAge()).append("\n");
}
System.out.println(sb);
}
}
可以通过 getRuntime 方法获取。
| 方法 | 作用 |
|---|---|
| exit() | 终止当前正在运行的 Java 虚拟机 |
| availableProcessors() | 获得CPU线程数 |
| maxMemory() | JVM能从系统中获取的总内存大小(单位byte) |
| totalMemory() | JVM已经从系统中获取的总内存大小(单位byte) |
| freeMemory() | JVM剩余内存大小(单位byte) |
| exec(String command) | 运行cmd命令 |
public class test54 {
public static void main(String[] args) {
Runtime r1=Runtime.getRuntime();
Runtime r2=Runtime.getRuntime();
System.out.println(r1==r2); //true
//获得CPU线程数
System.out.println(Runtime.getRuntime().availableProcessors()); //8
//JVM能从系统中获取的总内存大小(单位byte)
System.out.println(Runtime.getRuntime().maxMemory()/1024/1024); //3596 --- 3.5G
//JVM已经从系统中获取的总内存大小(单位byte)
System.out.println(Runtime.getRuntime().totalMemory()/1024/1024); //243M
//JVM剩余内存大小(单位byte)
System.out.println(Runtime.getRuntime().freeMemory()/1024/1024); //238M
Runtime.getRuntime().exit(0); //终止当前正在运行的虚拟机
}
}
import java.io.IOException;
public class test55 {
public static void main(String[] args) throws IOException {
//运行cmd命令
//shutdown:关机
//加上参数才能执行:
//-s :默认一分钟后关机
//-s -t指定关机时间:XXX秒后关机
//-a:取消关机操作
//-r:关机并重启
Runtime.getRuntime().exec("shutdown -s -t 3600"); //一个小时后关机
Runtime.getRuntime().exec("shutdown -a"); //取消关机
}
}
Java中的顶级父类,所有的类都直接或间接继承于Object
public Object(){
}
public Student() {
super(); //对于每一个类的空参构造隐含语句,默认访问父类的无参构造
}
| 方法 | 作用 |
|---|---|
| public String toString() | 该对象的字符串表示形式 |
| public boolean equals(Object obj) | 如果此对象与 obj 参数相同,则返回 true;否则返回 false |
| protected Object clone() | 此实例的一个副本 |
public class test {
public static void main(String[] args) {
Student s=new Student();
String str=s.toString();
System.out.println(str); //test11.Student@1b6d3586
System.out.println(s); //test11.Student@1b6d3586
}
}
//System源码
public final class System{
public final static PrintStream out = null;
}
//println源码
public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}
//String.valueOf源码
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
总结:
即默认情况下,打印对象调用Object的toString方法输出的是地址值
想要输出对象的属性值,即在对象所属的类中重写Object的toString方法
equals(Object obj) 默认比较的是对象的地址值
如果要想比较对象的属性值,则重写Object的equals方法
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
//重写Object的equals方法
@Override
public 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);
}
}
public class test56 {
public static void main(String[] args) {
String str="cjm是大傻逼";
StringBuilder sb=new StringBuilder("cjm是大傻逼");
System.out.println(str.equals(sb)); //false
System.out.println(sb.equals(str)); //false
}
}
//String的equals源码
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
String类重写的equals方法:
先判断是不是字符串,是再比较对象的内部属性值,不是直接return false
StringBuilder的equals方法直接继承于Object类,比较的是对象的地址值
clone()会帮我们创建一个对象,并把源对象中的数据拷贝过去
实现步骤:
public class Student implements Cloneable{
private String name;
private int age;
...
@Override
protected Object clone() throws CloneNotSupportedException {
//调用父类中的clone方法
//克隆一个对象并返回
return super.clone();
}
}
public class test {
public static void main(String[] args) throws CloneNotSupportedException {
Student stu=new Student("cjm",22);
Student stu1= (Student) stu.clone();
//因为clone返回的还是Object的克隆的对象,所以要强转成Student类型
System.out.println(stu1.getName()+stu1.getAge());
}
}
public interface Cloneable {
}

浅克隆出现的问题:

想要实现深克隆,要在类中重写clone方法
public class Student implements Cloneable{
private String name;
private int age;
private int[] arr=new int[10];
public Student() {
}
public Student(String name, int age, int[] arr) {
this.name = name;
this.age = age;
this.arr = arr;
}
...
public String getArr() {
StringBuilder sb=new StringBuilder();
sb.append("{");
for(int i=0;i<arr.length;i++){
if(i==arr.length-1){
sb.append(arr[i]);
}else{
sb.append(arr[i]).append(",");
}
}
sb.append("}");
return sb.toString();
}
public void setArr(int[] arr) {
this.arr = arr;
}
@Override
protected Object clone() throws CloneNotSupportedException {
int[] arrs=new int[arr.length];
for(int i=0;i<arr.length;i++){
arrs[i]=arr[i];
}
Student stu= (Student) super.clone(); //先用Object克隆一个新对象
stu.arr=arrs; //再替换Object对象的地址值
return stu; //将替换过后的克隆对象返回
}
}
public class test {
public static void main(String[] args) throws CloneNotSupportedException {
int[] arr={1,2,3,4,5,6,7,8,9,10};
Student stu=new Student("cjm",22,arr);
Student stu1= (Student) stu.clone();
//因为clone返回的还是Object的克隆的对象,所以要强转成Student类型
arr[0]=100;
System.out.println(stu.getName()+" "+stu.getAge()+" "+stu.getArr());
//cjm 22 {100,2,3,4,5,6,7,8,9,10}
System.out.println(stu1.getName()+" "+stu1.getAge()+" "+stu1.getArr());
//cjm 22 {1,2,3,4,5,6,7,8,9,10}
}
}
public class test {
public static void main(String[] args) throws CloneNotSupportedException {
int[] arr={1,2,3,4,5,6,7,8,9,10};
Student stu=new Student("cjm",22,arr);
Gson gson=new Gson();
String str=gson.toJson(stu); //将克隆的对象变成一个字符串
System.out.println(str);
//{"name":"cjm","age":22,"arr":[100,2,3,4,5,6,7,8,9,10]}
Student stu1=gson.fromJson(str,Student.class); //返回克隆后的对象
arr[0]=100;
System.out.println(stu.getName()+" "+stu.getAge()+" "+stu.getArr());
//cjm 22 {100,2,3,4,5,6,7,8,9,10}
System.out.println(stu1.getName()+" "+stu1.getAge()+" "+stu1.getArr());
//cjm 22 {1,2,3,4,5,6,7,8,9,10}
}
}
| 方法 | 功能 |
|---|---|
| equals(a,b) | 先判断是否为空,空和不相等返回false,相等返回true |
| isNull() | 判断对象是否为null,是null返回true,否返回false |
| nonNull() | 判断对象是否为null,不是null返回true,null返回false |
import java.util.Objects;
public class test {
public static void main(String[] args) throws CloneNotSupportedException {
int[] arr={1,2,3,4,5,6,7,8,9,10};
Student stu=new Student("cjm",22,arr);
Student stu2=null;
Student stu3=null;
System.out.println(Objects.equals(stu,stu2)); //false
System.out.println(Objects.equals(stu2,stu3)); //true
}
}
先判断是否为null,否,则调用对象的equals方法进行比较
注意:
==比较两个对象比较的是地址值
| 方法 | 说明 |
|---|---|
| public BigInteger(int num,Random rnd) | 获取随机大整数,范围:[0~2的num次方-1] |
| public BigInteger(String val) | 获取指定的大整数 |
| public BigInteger(String val,int radix) | 获取指定进制的大整数 |
| public static BigInteger valueOf(long val) | 静态方法获取BigInteger对象,内部有优化 |
对象一旦创建,内部记录的值不能改变
import java.math.BigInteger;
import java.util.Random;
public class test {
public static void main(String[] args) {
BigInteger bi1=new BigInteger(4,new Random()); //[0,15]
//或者...
Random r=new Random();
BigInteger bi2=new BigInteger(4,r);
}
}
import java.math.BigInteger;
import java.util.Random;
public class test {
public static void main(String[] args) {
//传入的字符串必须是整数,否则会报错
BigInteger bi3=new BigInteger("1000000000000000000000000000000000000000000000");
//BigInteger bi3=new BigInteger("10.0");————报错
//BigInteger bi3=new BigInteger("abc");————报错
}
}
public class test {
public static void main(String[] args) {
//字符串的数字必须是整数
//字符串中的数字必须与进制吻合,否则报错
BigInteger bi4 = new BigInteger("11", 2);
System.out.println(bi4); //3
}
}
package test14;
import java.math.BigInteger;
import java.util.Random;
public class test {
public static void main(String[] args) {
//能表示的范围比较小,只能在long范围以内的整数,超过范围则报错
//在内部对常用数字 -16~16 进行优化:
//先创建好 -16~16 BigInteger的对象,多次获取不会创建新的对象
BigInteger bi5 = BigInteger.valueOf(988888888888888888L);
System.out.println(bi5); //988888888888888888
BigInteger bi6 = BigInteger.valueOf(16);
BigInteger bi7 = BigInteger.valueOf(16);
System.out.println(bi6 == bi7); //true
BigInteger bi8 = BigInteger.valueOf(17);
BigInteger bi9 = BigInteger.valueOf(17);
System.out.println(bi8 == bi9); //false
}
}
import java.math.BigInteger;
import java.util.Random;
public class test {
public static void main(String[] args) {
BigInteger bi8 = BigInteger.valueOf(17);
BigInteger bi9 = BigInteger.valueOf(17);
BigInteger bi10=bi8.add(bi9);
System.out.println(bi10); //34
}
}
总结:
1.如果BigInteger表示的数字没有超出long范围,用静态方法valueOf获取
2.如果BigInteger表示的数字超出long范围,用构造方法BigInteger bi=new BigInteger(“”)获取
3.对象一旦创建,BigInteger内部记录的值不能发生改变
4.只要是计算都会产生一个新的BigInteger对象
| 方法 | 说明 |
|---|---|
| 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[] divideAndRemainder(BigInteger val) | 除法,获取数组:商是初始元素,余数是最终元素 |
| public BigInteger pow(int exponent) | 次幂 |
| public BigInteger gcd(BigInteger val) | 返回二者的最大公约数 |
| public BigInteger abs() | 绝对值 |
| public BigInteger negate() | 负数 |
| public BigInteger mod(BigInteger m) | 返回其值为 (this mod m) 的 BigInteger,此方法不同于 remainder,因为它始终返回一个非负BigInteger |
| public boolean equals(Object x) | 比较是否相同 |
| public BigInteger min(BigInteger val) | 最小值 |
| public BigInteger max(BigInteger val) | 最大值 |
| public int intValue() | 将此 BigInteger转换为int,超出范围数据有误 |
| public long longValue() | 将此 BigInteger 转换为 long,超出范围数据有误 |
| public double doubleValue() | 将此 BigInteger 转换为 double,太大精度丢失 |
注意:
如果此 BigInteger 太长而不适合用 int 表示,则仅返回 32 位的低位字节。
注意,此转换会丢失关于该 BigInteger 值的总大小的信息,并返回带有相反符号的结果。
signum表示BigInteger符号的正负:
signum+BigInteger mag数组就可以表示一个大整数

| 类型 | 占用字节 | 总bit位数 | 小数部分bit位数 |
|---|---|---|---|
| float | 4 | 32 | 23 |
| double | 8 | 64 | 52 |

| 方法 | 说明 |
|---|---|
| public BigDecimal(double val) | 将double转换为BigDecimal,不精确 |
| public BigDecimal(String val) | 将BigDecimal的字符串表示形式转换为BigDecimal,精确 |
| public static BigDecimal valueOf(long val) | 静态方法获取对象 |
说明:
1.如果表示的数字不大,没有超出double的范围,用静态方法获取对象
2.如果表示的数字比较大,超出double的范围,用构造方法获取对象
3.double的范围:-1.798E308~1.798E308
4.对于BigDecimal.valueOf,如果传递的是[0,10]之间的整数,方法会返回已经创建好的对象,不会重新new BigDecimal对象(注意!!!是整数)
| 方法 | 说明 |
|---|---|
| public BigDecimal abs() | 绝对值 |
| public BigDecimal add(BigDecimal augend) | 加法 |
| public BigDecimal subtract(BigDecimal subtrahend) | 减法 |
| public BigDecimal multiply(BigDecimal multiplicand) | 乘法 |
| public BigDecimal divide(BigDecimal divisor) | 除法,除不尽会报错 |
| public BigDecimal divide(BigDecimal divisor,精确几位,舍入模式) | 除法 |
| 舍入模式RoundingMode. | 说明 |
|---|---|
| UP | 远离零方向舍入 |
| DOWN | 向零方向舍入 |
| CEILING | 向正无限大方向舍入1.1->2,1.6->2 |
| FLOOR | 向负无限大方向舍入 -1.1->-2,-1.6->-2 |
| HALF_UP | 向最接近数字方向舍入,即四舍五入 |
| HALF_DOWN | 向最接近数字方向舍入,即五舍六入 2.5->2 2.6->3 |
| HALF_EVEN | 向最接近数字方向舍入,如果与两个相邻数字的距离相等,则向相邻的偶数舍入 2.5->2 5.1->6 |
正负号和小数点都是用ASCII码表示存入数组


| 表达式 | 说明 |
|---|---|
| [abc] | 只能是a,b或c |
| [^abc] | 除了a,b,c之外的任何字符 |
| [a-zA-Z] | a-z A-Z |
| [a-d[m-p]] | a-d或m-p |
| [a-z&&[def]] | a-z和def的交集,为:d、e、f |

public class test57 {
public static void main(String[] args) {
System.out.println("ab".matches("[abc]")); //false
System.out.println("ab".matches("[abc][abc]")); //true
//一个&在正则表达式中是一个符号
System.out.println("&".matches("[a-z&[abc]]")); //true
System.out.println("0".matches("a-z&&[abc]")); //false 0不在a-z与abc的交集里面
}
}
| 表达式 | 说明 |
|---|---|
| . | 任何字符 |
| \d | 一个数字[0-9] |
| \D | 非数字 |
| \s | 一个空白字符[\t\n\x0B\f\r] |
| \S | 非空白字符 |
| \w | [a-zA-Z_0-9]英文、数字、下划线 |
| \W | [^\w] |
\ 转义字符,改变后面字符的含义
\ 前面的\是一个转义字符,把后面的\变成一个普通的无含义字符(路径)
public class test57 {
public static void main(String[] args) {
// \ 转义字符,改变后面字符的含义
// \\ 前面的\是一个转义字符,把后面的\变成一个普通的无含义字符(路径)
System.out.println("\""); //"
System.out.println("3".matches("\\d")); //true
System.out.println("2333".matches("\\d\\d\\d\\d")); //true
}
}
| 表达式 | 说明 |
|---|---|
| X? | X,1次或0次 |
| X* | X,0次或多次 |
| X+ | X,1次或多次 |
| X{n} | X,正好n次 |
| X{n,} | X,至少n次 |
| X{n,m} | X,至少n但不超过m次 |
正则表达式更多内容查看PatternAPI帮助文档
