Object的成员方法:
Protected Object clone(int a):把A对象的属性值完全拷贝给B对象,也叫做对象拷贝,对象复制java内的克隆方式有两种:
浅克隆/浅拷贝:特点是直接拷贝,基本数据范例直接拷贝变量记载的数值,引用数据范例拷贝变量记载的地点值:
新的数组与原来数组管理同一个,当对其中一个做出改变,另一个也会相应改变
深克隆/深拷贝:特点基本数据范例直接拷贝变量记载的数值,引用数据范例拷贝变量记载的地点值,但是数组拷贝会重新创建一个新的数组,将原来数组值赋值给新数组:
区分深克隆与浅克隆:将克隆后改变的数组与克隆前的数组作对比,看是否改变
使用第三方工具实现克隆:
- //第三方的工具
- //1.第三方写的代码导入到项目中
- //2.编写代码
- Gson gson = new Gson();
- //把对象变成一个字符串
- String s = gson.toJson(u1);
- //再把字符串变回对象就可以了
- User user = gson.fromJson(s, User.class);
- //打印对象
- System.out.println(user);
复制代码
Objects的成员方法:
- //1.创建学生类的对象
- Student s1 = null;
- Student s2 = new Student(name:"zhangsan",age:23);
- //2.比较两个对象的属性值是否相同
- /* if(s1 != null){
- boolean result = s1.equals(s2);
- System.out.println(result);
- else{
- System.out.println("调用者为空");
- }*/
- boolean result = Objects.equals(s1, s2);
- System.out.println(result);
复制代码 细节:1.方法的底层会判定s1是否为null,如果为null,直接返回false
2.如果s1不为null,那么就使用s1再次调用equals方法
3.此时s1是Student范例,以是最终照旧会调用student中的equals方法。如果没有重写,比较地点值,如果重写了,就比较属性值。
- //public static boolean isNull(Object obj)判断对象是否为null,为null返回true,反之
- Student s3 = new Student();
- Student s4 = null;
- System.out.println(Objects.isNull(s3));//false
- System.out.println(Objects.isNull(s4));//true
- System.out.println(Objects.nonNull(s3));//true
- System.out.println(Objects.nonNull(s4));//false
复制代码
BigInteger构造方法:
BigInteger细节:对象一旦创建,内部记载的值不能发生改变
- //1.获取一个随机的大整数
- Random r = new Random();
- for (inti= 0; i< 100; i++) {
- BigInteger bd1 = new BigInteger(4,r);
- System.out.println(bd1);//[0~15]
- }
复制代码- //2.获取一个指定的大整数
- //细节:字符串中必须是整数,否则会报错
- BigInteger bd2 = new BigInteger("1.1");
- System.out.println(bd2);//报错
- BigInteger bd3 = new BigInteger("abc");
- System.out.println(bd3);//报错
复制代码- //3.获取指定进制的大整数
- BigInteger bd4 = new BigInteger("010100111",2);
- System.out.println(bd4);
- BigInteger bd5 = new BigInteger("123",2);
- System.out.println(bd5);//报错
复制代码 细节: 1.字符串中的数字必须是整数 2.字符串中的数字必须要跟进制吻合。比如二进制中,那么只能写e和1,写其他的就报错。
- //4.静态方法获取BigInteger的对象,内部有优化
- //L表示long
- BigInteger bd5 = BigInteger.valueOf(9223372036854775807L);
- System.out.println(bd5);
- BigInteger bd5 = BigInteger.valueOf(16);
- BigInteger bd6 = BigInteger.valueOf(16);
- System.out.println(bd5==bd6);//true
- BigInteger bd7 = BigInteger.valueOf(17);
- BigInteger bd8 = BigInteger.valueOf(17);
- System.out.println(bd7==bd8);//false
复制代码 细节:1.能表树模围比较小,在1ong的取值范围之类,如果超出1ong的范围就不行了。2.在内部对常用的数字:-16~16举行了优化。提前把-16~16先创建好BigInteger的对象,如果多次获取不会重新创建新的。
- //5.对象一旦创建内部的数据不能发生改变
- BigInteger bd9 = BigInteger.valueOf(1);
- BigInteger bd1e = BigInteger.valueOf(2);
- BigInteger result = bd9.add(bd10);
- System.out.println(result);//3
- //此时,不会修改参与计算的BigInteger对象中的值,而是产生了一个新的BigInteger对象记录3
- System.out.println(bd9==result);//false
- System.out.println(bd10==result);//false
复制代码
BigInteger常见成员方法:
- //1.创建两个BigInteger对象
- BigInteger bd1 = BigInteger.valueOf(1O);
- BigInteger bd2 = BigInteger.valueOf(5);
- //2.加法
- BigInteger bd3 = bd1.add(bd2);
- System.out.println(bd3);
- //后面三个都是这样
- //3.除法,获取商和余数
- BigInteger[] arr = bd1.divideAndRemainder(bd2);
- System.out.println(arr[0]);
- System.out.println(arr[1]);
- //4.比较是否相同
- boolean result = bd1.equals(bd2);
- System.out.println(result);
- //5.次幂
- BigInteger bd4 = bd1.pow(2);
- System.out.println(bd4);
- //6.max
- BigInteger bd5 = bd1.max(bd2);
- System.out.println(bd5 == bd1);//true
- System.out.println(bd5 == bd2);//false
- //7.转为int类型整数,超出范围数据有误
- /* BigInteger bd6 = BigInteger.valueOf(2147483647L);
- int i = bd6.intValue();
- System.out.println(i);*/
- BigInteger bd6 = BigInteger.valueOf(200);
- double V = bd6.doubleValue();
- System.out.println(v);//200.0
复制代码 扩展: BigInteger底层储存方式:
BigInteger存储上限:
数组的最大长度是int的最大值:2147483647
数组中每一位能表示的数字:-2147483648~2147483647
数组中最多能存储元素个数:21亿多
数组中每一位能表示的数字:42亿多
BigInteger能表示的最大数字为:42亿的21亿次方
引入BigDecima:
BigDecima的作用: 用于小数的准确计算 用来表示很大的小数
- //1.通过传递double类型的小数来创建对象
- /细节:
- //这种方式有可能是不精确的,所以不建议使用
- BigDecimal bd1 = new BigDecimal(O.01);
- BigDecimal bd2 = new BigDecimal(O.09);
- System.out.println(bd1);
- System.out.println(bd2);
- //2.通过传递字符串表示的小数来创建对象
- BigDecimal bd3 = new BigDecimal("0.01");
- BigDecimal bd4 = new BigDecimal("O.09");
- BigDecimal bd5 = bd3.add(bd4);
- System.out.println(bd3);
- System.out.println(bd4);
- System.out.println(bd5);
- //3.通过静态方法获取对象
- BigDecimal bd6 = BigDecimal.valueOf(10);
- System.out.println(bd6);
复制代码 细节:
1.如果要表示的数字不大,没有超出double的取值范围,建议使用静态方法
2.如果要表示的数字比较大,超出了double的取值范围,建议使用构造方法
3.如果我们传递的是0~10之间的整数,包含0,包含10,那么方法会返回己经创建好的对象,不会重新new
BigDecima的使用:
- //1.加法
- BigDecimal bd1 = BigDecimal.valueOf(10.0);
- BigDecimal bd2 = BigDecimal.valueOf(3.0);
- BigDecimal bd3 = bd1.add(bd2);
- System.out.println(bd3);//12.0
- //2.减法
- BigDecimal bd4 = bd1.subtract(bd2);
- System.out.println(bd4);//8.0
- //3.乘法
- BigDecimal bd5 = bd1.multiply(bd2);
- System.out.println(bd5);//20.00
- //4.除法
- BigDecimal bd6 = bd1.divide(bd2,scale:2,RoundingMode.HALF_UP);
- System.out.println(bd6);//3.33
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |