Object类
1、equals()方法。
<1>比较两个引用是否指向同一个对象。
<2>被比较的两个引用所指向的对象的状态(或属性)是否相同。
注:==在比较String对象时比较的是同一对象才为true,而equals是在比较同一对象、或具有相同状态的两个对像时都为真。
用法:
String str1="hello";
String str2="hello";
String str3=new String("hello");
System.out.println(str1.equals(str2));//true
System.out.println(str1.equals(str3));//true
2、hashCode()方法。
主要就是由对象导出一个hashCode值。hashCode值由对象的状态决定,只要对象的状态相同即对象的hashCode值就相同。
用法:
String str1="hello";
String str2=new String("hello");
String str3="world";
System.out.println(str1.hashCode());
System.out.println(str2.hashCode());
System.out.println(str3.hashCode());
//注:str1和str2的对象不是同一个对象,但是它们两
//个对象的状态(即属性)都是相同的,因此str1
//和str2的hashCode值是相等的。有由于str3对象的状
//态与str1、str2不同,因此str1、str2与str3的
//hashCode值是不同的。
3、toString()方法
将对象转换成有实际意义的字符串。