| 内容 | 作用 |
|---|---|
| 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 ~ 2147483647System.out.println(Math.abs(-2147483648)); //-2147483648System.out.println(Math.absExact(-2147483648)); //JDK15以后,会报错}
}
public class test53 {public static void main(String[] args) {System.out.println(Math.ceil(13.34)); //14.0System.out.println(Math.ceil(13.94)); //14.0System.out.println(Math.ceil(-13.34)); //-13.0System.out.println(Math.ceil(-13.94)); //-13.0System.out.println(Math.floor(13.34)); //13.0System.out.println(Math.floor(13.94)); //13.0System.out.println(Math.floor(-13.34)); //-14.0System.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); //238MRuntime.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@1b6d3586System.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方法@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);}}
public class test56 {public static void main(String[] args) {String str="cjm是大傻逼";StringBuilder sb=new StringBuilder("cjm是大傻逼");System.out.println(str.equals(sb)); //falseSystem.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;...@Overrideprotected 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;iif(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;}@Overrideprotected Object clone() throws CloneNotSupportedException {int[] arrs=new int[arr.length];for(int i=0;iarrs[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)); //falseSystem.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); //988888888888888888BigInteger bi6 = BigInteger.valueOf(16);BigInteger bi7 = BigInteger.valueOf(16);System.out.println(bi6 == bi7); //trueBigInteger 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 值的总大小的信息,并返回带有相反符号的结果。
下一篇:缅甸这个词是什么意思