java常用类解析一:System类、Object类、Arrays类、Cloneable接口

  1. <span style="font-size:16px;">package test;  
  2. public class SystemDemo {  
  3.     public static void main(String[] args) {  
  4.         String[] s = new String[] { "liu" };  
  5.         String[] s2 = new String[] { "hai" };  
  6.         String[][] a = { s, s2, s, s, s, s2, s2, s };  
  7.         String[][] b = new String[10][10];  
  8.         /* 
  9.          * System类包含一些有用的类字段和方法,是final类,无法被继承,构造方法是private,不能创建System对象。 
  10.          * 所有public属性和方法都是final static 
  11.          */  
  12.         // 1.数组复制,采用本地方法复制,实现了深拷贝  
  13.         System.arraycopy(a, 1, b, 05);  
  14.         System.out.println(b[0][0]);  
  15.         // 2.已经过去的毫米数,从1970-1-1开始  
  16.         System.currentTimeMillis();  
  17.         // 3.提示虚拟机进行垃圾回收,通过调用Runtime.getRuntime().gc();实现  
  18.         System.gc();  
  19.         // 4.返回系统环境  
  20.         System.getenv();  
  21.         // 5.返回当前的系统属性。  
  22.         System.getProperties();  
  23.         // 6.可用於計數已過去的時間  
  24.         System.nanoTime();  
  25.         // 7.0表示正常退出,調用Runtime.getRuntime().exit(0);  
  26.         System.exit(0);  
  27.         // 8.返回给定对象的哈希码,该代码与默认的方法 hashCode() 返回的代码一样,无论给定对象的类是否重写 hashCode()。  
  28.         System.identityHashCode(null);  
  29.     }  
  30. }  
  31. </span>  

  1. <span style="font-size:16px;">package test;  
  2.   
  3. public class ObjectDemo {  
  4.     /* 
  5.      * 1.Object没有public 的静态属性和方法  
  6.      * 2.public final native Class<?> getClass()返回运行时类信息, 
  7.      * 3.toString 返回类名+@+哈希码值 
  8.      * 4.其中wait和notify方法是final的,不可继承 
  9.      * 5.equals方法只比较对象的引用,hashCode方法返回哈希码值。 
  10.      * 6.重写equals方法要重写hashCode,因为相同的对象(通过equals比较返回true) 
  11.      *   必须返回相同的哈希码。 
  12.      * 7.finalize方法是一个protected空方法 
  13.      * 8.protected native Object clone()返回一个副本,对象必须实现Cloneable接口 
  14.      */  
  15. }  
  16. </span>  

  1. <span style="font-size:16px;">package test;  
  2.   
  3. import java.util.Arrays;  
  4.   
  5. public class ArraysDemo {  
  6.     /* 
  7.      * 1.数组类提供了排序功能,对基本数据类型length<7采用直接插入排序,否则采用快速排序 如果数组元素时对象,采用合并排序 
  8.      * 2.提供二分查找法实现,注意二分查找时先对数组进行排序,否则返回一个不确定值 
  9.      */  
  10.     public static void main(String[] args) {  
  11.         int[][] a = { { 12 } };  
  12.         int[][] b = { { 12 } };  
  13.         System.out.println(Arrays.toString(a));  
  14.         // 3. 多维数组的toString  
  15.         System.out.println(Arrays.deepToString(a));  
  16.         System.out.println(Arrays.hashCode(a));  
  17.         System.out.println(Arrays.deepHashCode(a));  
  18.         System.out.println(Arrays.equals(a, b));  
  19.         // 4. 多维数组的比较  
  20.         System.out.println(Arrays.deepEquals(a, b));  
  21.   
  22.         // 5. 并没有实现多维数组的复制  
  23.         int[][] c = Arrays.copyOf(a, 1);  
  24.         // 6.填充数组  
  25.         Arrays.fill(a[0], 5);// 在此改变a的值影响到了数组c的值  
  26.         System.out.println(Arrays.deepToString(c));  
  27.         System.out.println(Arrays.equals(a, c));  
  28.         System.out.println(Arrays.deepEquals(a, c));  
  29.   
  30.     }  
  31. }  
  32. </span>  

  1. <span style="font-size:16px;">package test;  
  2.   
  3. public class DeepCloneDemo {  
  4.     public static void main(String[] args) {  
  5.         B b = new B(2new A(1));  
  6.         B b1 = (B) b.clone();  
  7.         System.out.println(b == b1);  
  8.         System.out.println(b.equals(b1));  
  9.         System.out.println(b.getClass() == b.getClass());  
  10.         System.out.println("改变b的副本b1前:y=" + b.getY() + ",x=" + b.getA().getX());  
  11.         b1.setY(5);  
  12.         b1.getA().setX(100);  
  13.         System.out.println("改变b的副本b1后:y=" + b.getY() + ",x=" + b.getA().getX());  
  14.         System.out.println("深克隆成功!!!");  
  15.     }  
  16. }  
  17.   
  18. class A implements Cloneable {  
  19.     private int x;  
  20.   
  21.     // 为了实现深克隆  
  22.     public Object clone() {  
  23.         A a = null;  
  24.         try {  
  25.             a = (A) super.clone();  
  26.         } catch (CloneNotSupportedException e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.         return a;  
  30.     }  
  31.   
  32.     public A(int x) {  
  33.         this.x = x;  
  34.     }  
  35.   
  36.     public int getX() {  
  37.         return x;  
  38.     }  
  39.   
  40.     public void setX(int x) {  
  41.         this.x = x;  
  42.     }  
  43. }  
  44.   
  45. class B implements Cloneable {  
  46.     private int y;  
  47.     private A a;  
  48.   
  49.     // 覆盖Object中clone方法  
  50.     // protected native Object clone() throws CloneNotSupportedException;  
  51.     // 注意到protected,这里把权限改为了public  
  52.     public Object clone() {  
  53.         B b = null;  
  54.         try {  
  55.             b = (B) super.clone();  
  56.             // 实现深克隆,没有这条语句只是克隆了a的引用  
  57.             b.a = (A) a.clone();  
  58.         } catch (CloneNotSupportedException e) {  
  59.             e.printStackTrace();  
  60.         }  
  61.         return b;  
  62.     }  
  63.   
  64.     public B(int y, A a) {  
  65.         this.y = y;  
  66.         this.a = a;  
  67.     }  
  68.   
  69.     public int getY() {  
  70.         return y;  
  71.     }  
  72.   
  73.     public A getA() {  
  74.         return a;  
  75.     }  
  76.   
  77.     public void setY(int y) {  
  78.         this.y = y;  
  79.     }  
  80.   
  81.     public void setA(A a) {  
  82.         this.a = a;  
  83.     }  
  84. }  
  85. </span> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值