4月1号.

打印 上一主题 下一主题

主题 1647|帖子 1647|积分 4941

Object的成员方法:

Protected  Object  clone(int a):把A对象的属性值完全拷贝给B对象,也叫做对象拷贝,对象复制
java内的克隆方式有两种:

浅克隆/浅拷贝:特点是直接拷贝,基本数据范例直接拷贝变量记载的数值,引用数据范例拷贝变量记载的地点值:
新的数组与原来数组管理同一个,当对其中一个做出改变,另一个也会相应改变


深克隆/深拷贝:特点基本数据范例直接拷贝变量记载的数值,引用数据范例拷贝变量记载的地点值,但是数组拷贝会重新创建一个新的数组,将原来数组值赋值给新数组:
 

区分深克隆与浅克隆:将克隆后改变的数组与克隆前的数组作对比,看是否改变
使用第三方工具实现克隆:
  1. //第三方的工具
  2. //1.第三方写的代码导入到项目中
  3. //2.编写代码
  4. Gson gson = new Gson();
  5. //把对象变成一个字符串
  6. String s = gson.toJson(u1);
  7. //再把字符串变回对象就可以了
  8. User user = gson.fromJson(s, User.class);
  9. //打印对象
  10. System.out.println(user);
复制代码

Objects的成员方法:

  1. //1.创建学生类的对象
  2.     Student s1 = null;
  3.     Student s2 = new Student(name:"zhangsan",age:23);
  4. //2.比较两个对象的属性值是否相同
  5. /*  if(s1 != null){
  6.        boolean result = s1.equals(s2);
  7.        System.out.println(result);
  8.        else{
  9.            System.out.println("调用者为空");
  10.        }*/
  11.      boolean result = Objects.equals(s1, s2);
  12.      System.out.println(result);
复制代码
细节:1.方法的底层会判定s1是否为null,如果为null,直接返回false
           2.如果s1不为null,那么就使用s1再次调用equals方法
           3.此时s1是Student范例,以是最终照旧会调用student中的equals方法。如果没有重写,比较地点值,如果重写了,就比较属性值。

  1. //public static boolean isNull(Object obj)判断对象是否为null,为null返回true,反之
  2. Student s3 = new Student();
  3. Student s4 = null;
  4. System.out.println(Objects.isNull(s3));//false
  5. System.out.println(Objects.isNull(s4));//true
  6. System.out.println(Objects.nonNull(s3));//true
  7. System.out.println(Objects.nonNull(s4));//false
复制代码

 BigInteger构造方法:

  BigInteger细节:对象一旦创建,内部记载的值不能发生改变
  1. //1.获取一个随机的大整数
  2. Random r = new Random();
  3. for (inti= 0; i< 100; i++) {
  4.     BigInteger bd1 = new BigInteger(4,r);
  5.     System.out.println(bd1);//[0~15]
  6. }
复制代码
  1. //2.获取一个指定的大整数
  2. //细节:字符串中必须是整数,否则会报错
  3. BigInteger bd2 = new BigInteger("1.1");
  4. System.out.println(bd2);//报错
  5. BigInteger bd3 = new BigInteger("abc");
  6. System.out.println(bd3);//报错
复制代码
  1. //3.获取指定进制的大整数
  2. BigInteger bd4 = new BigInteger("010100111",2);
  3. System.out.println(bd4);
  4. BigInteger bd5 = new BigInteger("123",2);
  5. System.out.println(bd5);//报错
复制代码
细节: 1.字符串中的数字必须是整数  2.字符串中的数字必须要跟进制吻合。比如二进制中,那么只能写e和1,写其他的就报错。
  1. //4.静态方法获取BigInteger的对象,内部有优化
  2. //L表示long
  3. BigInteger bd5 = BigInteger.valueOf(9223372036854775807L);
  4. System.out.println(bd5);
  5. BigInteger bd5 = BigInteger.valueOf(16);
  6. BigInteger bd6 = BigInteger.valueOf(16);
  7. System.out.println(bd5==bd6);//true
  8. BigInteger bd7 = BigInteger.valueOf(17);
  9. BigInteger bd8 = BigInteger.valueOf(17);
  10. System.out.println(bd7==bd8);//false
复制代码
细节:1.能表树模围比较小,在1ong的取值范围之类,如果超出1ong的范围就不行了。2.在内部对常用的数字:-16~16举行了优化。提前把-16~16先创建好BigInteger的对象,如果多次获取不会重新创建新的。
  1. //5.对象一旦创建内部的数据不能发生改变
  2. BigInteger bd9 = BigInteger.valueOf(1);
  3. BigInteger bd1e = BigInteger.valueOf(2);
  4. BigInteger result = bd9.add(bd10);
  5. System.out.println(result);//3
  6. //此时,不会修改参与计算的BigInteger对象中的值,而是产生了一个新的BigInteger对象记录3
  7. System.out.println(bd9==result);//false
  8. System.out.println(bd10==result);//false
复制代码

   BigInteger常见成员方法:

  1. //1.创建两个BigInteger对象
  2. BigInteger bd1 = BigInteger.valueOf(1O);
  3. BigInteger bd2 = BigInteger.valueOf(5);
  4. //2.加法
  5. BigInteger bd3 = bd1.add(bd2);
  6. System.out.println(bd3);
  7. //后面三个都是这样
  8. //3.除法,获取商和余数
  9. BigInteger[] arr = bd1.divideAndRemainder(bd2);
  10. System.out.println(arr[0]);
  11. System.out.println(arr[1]);
  12. //4.比较是否相同
  13. boolean result = bd1.equals(bd2);
  14. System.out.println(result);
  15. //5.次幂
  16. BigInteger bd4 = bd1.pow(2);
  17. System.out.println(bd4);
  18. //6.max
  19. BigInteger bd5 = bd1.max(bd2);
  20. System.out.println(bd5 == bd1);//true
  21. System.out.println(bd5 == bd2);//false
  22. //7.转为int类型整数,超出范围数据有误
  23. /* BigInteger bd6 = BigInteger.valueOf(2147483647L);
  24. int i = bd6.intValue();
  25. System.out.println(i);*/
  26. BigInteger bd6 = BigInteger.valueOf(200);
  27. double V = bd6.doubleValue();
  28. System.out.println(v);//200.0
复制代码
扩展:  BigInteger底层储存方式:

  BigInteger存储上限:
数组的最大长度是int的最大值:2147483647
数组中每一位能表示的数字:-2147483648~2147483647

数组中最多能存储元素个数:21亿多
数组中每一位能表示的数字:42亿多

BigInteger能表示的最大数字为:42亿的21亿次方

引入BigDecima:
 
 BigDecima的作用:  用于小数的准确计算   用来表示很大的小数
  1. //1.通过传递double类型的小数来创建对象
  2. /细节:
  3. //这种方式有可能是不精确的,所以不建议使用
  4. BigDecimal bd1 = new BigDecimal(O.01);
  5. BigDecimal bd2 = new BigDecimal(O.09);
  6. System.out.println(bd1);
  7. System.out.println(bd2);
  8. //2.通过传递字符串表示的小数来创建对象
  9. BigDecimal bd3 = new BigDecimal("0.01");
  10. BigDecimal bd4 = new BigDecimal("O.09");
  11. BigDecimal bd5 = bd3.add(bd4);
  12. System.out.println(bd3);
  13. System.out.println(bd4);
  14. System.out.println(bd5);
  15. //3.通过静态方法获取对象
  16. BigDecimal bd6 = BigDecimal.valueOf(10);
  17. System.out.println(bd6);
复制代码
细节:
  1.如果要表示的数字不大,没有超出double的取值范围,建议使用静态方法
  2.如果要表示的数字比较大,超出了double的取值范围,建议使用构造方法
  3.如果我们传递的是0~10之间的整数,包含0,包含10,那么方法会返回己经创建好的对象,不会重新new
 BigDecima的使用:

  1. //1.加法
  2. BigDecimal bd1 = BigDecimal.valueOf(10.0);
  3. BigDecimal bd2 = BigDecimal.valueOf(3.0);
  4. BigDecimal bd3 = bd1.add(bd2);
  5. System.out.println(bd3);//12.0
  6. //2.减法
  7. BigDecimal bd4 = bd1.subtract(bd2);
  8. System.out.println(bd4);//8.0
  9. //3.乘法
  10. BigDecimal bd5 = bd1.multiply(bd2);
  11. System.out.println(bd5);//20.00
  12. //4.除法
  13. BigDecimal bd6 = bd1.divide(bd2,scale:2,RoundingMode.HALF_UP);
  14. System.out.println(bd6);//3.33
复制代码
 


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

滴水恩情

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表