package NeiBuLei; public class hashCode { public static void main(String[] args) { AA aa = new AA(); AA aa2 = new AA(); AA aa3 = aa; AA aa4 = aa2; System.out.println("aa.hashCode()="+ aa.hashCode()); //aa.hashCode()=2129789493 System.out.println("aa2.hashCode()="+ aa2.hashCode()); //aa2.hashCode()=1313922862 System.out.println("aa3.hashCode()="+ aa3.hashCode()); //aa3.hashCode()=2129789493 System.out.println("aa4.hashCode()="+ aa4.hashCode()); //aa4.hashCode()=1313922862 } } class AA{}
package NeiBuLei; public class toString { public static void main(String[] args) { test test = new test(1,2,3); test.toString(); System.out.println(test.toString()); } } class test{ public int name; public int age; public int score; @Override public String toString() { return "test{" + "name=" + name + ", age=" + age + ", score=" + score + '}'; } public test() { } public test(int name,int age,int score) { this.name = name; this.age = age; this.score = score; } }
package NeiBuLei; //用于创建并返回一个对象的拷贝 // clone 方法是浅拷贝,对象内属性引用的对象只会拷贝引用地址,而不会将引用的对象重新分配内存,相对应的深拷贝则会连引用的对象也重新创建。 public class clone { public static void main(String[] args) { Charen p = new Charen(23, "zhang"); Charen p1 = p; System.out.println(p); //NeiBuLei.Charen@7ef20235 System.out.println(p1); //NeiBuLei.Charen@7ef20235 //打印的地址值是相同的,既然地址都是相同的,那么肯定是同一个对象。p和p1只是引用而已,他们都指向了一个相同的对象 // 可以把这种现象叫做引用的复制 Charen s = new Charen(23, "zhang"); Charen s1 = null; try { //要捕获 s1 = (Charen)s.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } //两个对象的地址是不同的,也就是说创建了新的对象, 而不是把原对象的地址赋给了一个新的引用变量 System.out.println(s); //NeiBuLei.Charen@27d6c5e0 System.out.println(s1); //NeiBuLei.Charen@4f3f5b24 } } class Charen implements Cloneable { int age; String name; public Charen() { } public Charen(int age,String name) { this.age = age; this.name = name; } @Override //重写方法 protected Object clone() throws CloneNotSupportedException { return (Charen)super.clone(); } }
String name 中name是String 引用,所以浅拷贝指向同一个name,深拷贝又复制了一个新的name,指向新复制的name。
package NeiBuLei; //当垃圾收集确定不再有对该对象的引用时,垃圾收集器在对象上调用该对象 //返回对象的类 public class getClass { public static void main(String[] args) { Object o1= new Object(); System.out.println(o1.getClass()); //class java.lang.Object A a = new A(); System.out.println(a.getClass()); //class NeiBuLei.A } } class A{}
//==: 如果判断的是基本类型,判断的是 值 是否相等 int x1 = 10; int x2 = 10; double x3 = 10.0; System.out.println(x1 == x2);//true System.out.println(x1 == x3);//true
package denghao; public class Test01 { public static void main(String[] args) { //==: 如果判断的是引用类型,判断的是地址是否相等,即判断是不是同一个对象 A a = new A(); A b = a; A c = b; System.out.println(a==c);// ? true System.out.println(b==c);// true B obj = a; System.out.println(obj==c);// true } } class B{} class A extends B{}
package NeiBuLei; public class ObjectLei { public static void main(String[] args) { Person p1 = new Person(); p1.name = "zyx"; Person p2 = new Person(); p2.name = "zyx"; //flase //对象不同 System.out.println(p1 == p2);// 引用类型——判断是否为同一个对象(地址) //true //字符串相同 System.out.println(p1.name.equals(p2.name));// p.name是String类型,重写了equals()方法——判断内容是否一样 //flase //对象不同 System.out.println(p1.equals(p2));//p1,p2属于Person类,该类并没有重写equals()方法(继承父类equals()方法,即判断地址) String s1 = new String("abc"); String s2 = new String("abc"); //true //字符串相同 System.out.println(s1.equals(s2)); //false //对象不同 System.out.println(s1 == s2); } } class Person{ public String name; }
package NeiBuLei; //run()和start()方法属于线程类Thread public class wait{ static Object locate=new Object(); public static class T1 extends Thread{ //T1线程 public void run(){ synchronized (locate){ //同步锁 System.out.println("T1 start"); try { System.out.println("T1等待locate对象"); locate.wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("T1 end"); } } } public static class T2 extends Thread{ //T2线程 public void run(){ synchronized (locate){ //同步锁 System.out.println("T2 被唤起"); locate.notify(); System.out.println("T2 end"); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public static void main(String[] args) { Thread t1=new T1(); Thread t2=new T2(); t1.start(); t2.start(); } }
T1 start T1等待locate对象 T2 被唤起 T2 end //过几秒 T1 end