常用的object类中的API
public int hashCode();返回该对象的哈希码值
public final class getClass() :
返回此Object的运行时类;class类的实例表示正在运行的java应用程序中的类和接口
哪个类的对象调用的getClass,那么发挥的就是这个对象所属于的哪个类所对应的class字节码文件
public String toString():
返回该对象的字符串表示。通常toSring方法会返回一个以文本方式表示此对象的字符串。结果应该是一个简明但易于读懂的信息表达式
建议所有子类都重写此方法
Object 类的 toString 方法返回一个字符串,该字符串由类名(对象是该类的一个实例)、at 标记符“@”和此对象哈希码的无符号十六进制表示组成。换句话说,该方法返回一个字符串,它的值等于:
getClass().getName() + '@' + Integer.toHexString(hashCode())
public boolean equals(Object obj)
Object类的equals方式:当且仅当xy引用同一个对象时,此方法才返回true;
换句话说比较的是两个对象的地址值(hashcode)
protected void finalize()
当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。子类重写finalize方法以配置系统资源或执行其他清除
finalize的常规协定是当java虚拟机已确定尚未终止的任何线程无法再通过任何方法访问此对象时,将调用此方法,除非由于准备终止的其他某个对象或类的终结操作执行了某个操作
对于任何给定的对象,java虚拟机最多只调用一次finalize方法,finalize 方法抛出的任何异常都会导致此对象的终结操作停止,但可以通过其他方法忽略它。
protected Object clone();
创建并返回该对象的一个副本;分浅拷贝和深拷贝
Object类
public class obiectClass { public static void main(String[] args) { Object o = new ABC(1); //getClass()\ System.out.println("class name=" + o.getClass().getName()); System.out.println("class name=" + o.getClass().getSimpleName()); //内存地址的一种表现形式 System.out.println("hashCode=" + o.hashCode()); System.out.println("Object o=" + o.toString()); System.out.println("Object o=" + o); System.out.println(o); //equals方法 代码中用==比较 Student student = new Student("zs", 18); Student student1 = new Student("zs", 18); System.out.println(student.equals(student)); BBC bbc=new BBC(100); Student origin=new Student("lisi",22,bbc); // Student clone=(Student) origin.clone(); // System.out.println("before modifie clone = " + clone); // origin.bbc.setI(1000); // System. out.println("after modifie clone="+clone); } } class ABC extends Object{ public ABC(int i) { this.i=i; } int i=10; @Override public String toString() { return "这是我自己定义的toString,为了描述一个ABC类型的对象" + i ; } } class Student implements Cloneable{ String name; int age; BBC bbc; public Student(String name, int age) { this.name = name; this.age = age; } public boolean equals(Object obj){ return this==obj; } public Student(String name, int age, BBC bbc) { this.name = name; this.age = age; this.bbc = bbc; } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } @Override protected void finalize() throws Throwable { System.out.println("我变成垃圾了,我要被回收了");; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", bbc=" + bbc + '}'; } } class BBC{ int i=10; public BBC(int i) { this.i=i; } public int getI() { return i; } public void setI(int i) { this.i = i; } @Override public String toString() { return "BBC{" + "i=" + i + '}'; } } class BBD{ int j; }
String类
package bamzhy.day15; /* 创建String类对象 public String(); public String (byte[]byte); public String (byte[]byte,int offset,int length) public String (char[] value); public String (char[] value,int offset,int count) public String(String original) 字符串的特性:字符串是常量,他们的值在创建之后不能更改 */ public class StringClass { public static void main(String[] args) { //第一种方式 没有数据 String s=new String(); //第二种方式abcd byte[]chars={97,98,99,100}; String byteString=new String(chars); System.out.println(byteString); //offset表示从字节中的哪个字节数据开始构造字符串 //length表示要用几个字节数组中的数据来构造字符串 // public void String(byte bytes[],int offset,int length) String bytesPartString=new String(chars,3,1); System.out.println("bytePartString="+bytesPartString); //public String (String original) //String类代表字符串。java程序中的所有字符串字面值("abc")都作为此类的实例实现 String hello=new String("hello"); System.out.println(hello); String ss="hello"; ss+="world";//字符串可以直接加 System.out.println("ss="+ss); } };
public class StringExercise { public static void main(String[] args) { String s1 = new String("hello"); String s2 = new String("hello"); System.out.println(s1==s2);//false System.out.println(s1.equals(s2));//true String s3 = new String("hello"); String s4 = "hello"; System.out.println(s3==s4);//false System.out.println(s3.equals(s4));//true String s5 = "hello"; String s6 = "hello"; System.out.println(s5==s6); //true System.out.println(s5.equals(s6));//true String s7 = "hello"; String s8 = "world"; String s9 = "helloworld"; //只要字符串凭借操作中,有变量形式参与,现在堆上开辟空间再在空间中进行凭借 System.out.println(s9==(s7+s8));//false System.out.println(s9.equals(s7+s8));//true System.out.println(s9=="hello"+"world"); System.out.println(s9.equals("hello"+"world")); } }