toString方法
Object中的api如下:
public String toString()
Returns a string representation of the object. In general, the toString
method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
The toString
method for class Object
returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@
', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
Returns:
a string representation of the object.
返回的是: 类名@java对象的内存地址经过哈希算法得出的int类型值再转换成十六进制,这个输出结果可以等同看做java对象在堆中的内存地址。
如果直接打印一个引用数据类型的对象,系统会默认调用其toString方法。因为只会返回一个对象的地址,所以实际使时,toString方法要被重写.
String类中已经重写了toString方法,返回的是值本身,api如下
public String toString()
This object (which is already a string!) is itself returned.
Specified by:
toString
in interface CharSequence
Overrides:
Returns:
the string itself.
public class ToStringTest {
public static void main(String[] args) {
int [] a1={1,2,3,4};
Syetem.out.println(a1);
Student s1=new Student("Li", 5);
System.out.println(s1.toString());
System.out.println(s1); //如果打印引用类型,默认调用引用类型的toString方法
String color="blue";
System.out.println(color);//String类中已经重写了toString方法,返回的是值
}
}
class Student{
int age;
String name;
//有参构造器
public Student(String name,int age) {
this.name=name;
this.age=age;
}
@Override
public String toString() {
// TODO Auto-generated method stub
//return super.toString();//原始的toString方法的方法体,调用父类的toString方法
return "Student[name="+this.name+", age="+this.age+"]" ;
}
}
out:
[I@512ddf17
Student[name=Li, age=5]
Student[name=Li, age=5]
blue
equals方法
equals方法的作用是,判断两个对象是否一样,在\Java\jdk-11.0.1\lib\src\java.base\java\lang的Object.java中源码是
public boolean equals(Object obj) {
return (this == obj);
}
在Object中这个方法中比较的是两个对象在堆内存的地址是否相同
"=="比较基本数据类型的值是否相同,对于引用类型,"=="比较堆内存地址是否相同,
在使用的时候,需要对equals重写,来比较成员变量值是否相同.
public class EqualsTest {
public static void main(String[] args) {
String name="Li";
String name1="Li";
System.out.println("name==name1 :"+(name==name1));//对引用数据类型,==比较的是内存地址,
//在创建String类型时,会把Li放进字符串缓冲池,所以name和name1指向的是同一个内存地址
System.out.println("name.equals(name1):"+name.equals(name1));//String类重写了equals方法,比较的是两个对象的值
String s1=new String("hello");
String s2=new String("hello");
System.out.println("s1==s2 : "+(s1==s2));//new新创建两个对象,那么内存地址是不一样的,false
System.out.println("s1.equals(s2) :"+s1.equals(s2));//String类的equals方法比较两个对象的值,所以是true
String s3=s1;
System.out.println("s1==s3 :"+(s1==s3));//s3指向s1的内存地址,true
Student2 s21=new Student2("Li", 5);
Student2 s22=new Student2("Li", 5);
System.out.println("s21==s22:"+(s21==s22));//内存地址不同,false
System.out.println("s21.equals(s22) :"+(s21.equals(s22)));//没有重写equals方法,比较的是两个对象的内存地址
Student1 s11=new Student1("Li", 6);
Student1 s12=new Student1("Li", 6);
System.out.println("s11==s12 :"+(s11==s12));//内存地址不同
System.out.println("s11.equals(s12):"+(s11.equals(s12)));//重写了equals方法,先比较地址,地址不同再比较各个值是否相同
}
}
class Student1{
String name;
int age;
public Student1(String name,int age) {
//有参构造方法
this.name=name;
this.age=age;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
//return super.equals(obj);重写equals方法
//如果两个对象的内存地址相同,返回true
if (this == obj) {
return true;
}
//如果被比较的对象不是Student1类型,尝试强制转化成Student类型
if (obj instanceof Student1) {
Student1 anObject=(Student1)obj;
//分别比较各个成员变量的值,==比较基本数据类型
//对引用数据类型,==比较的是内存地址,但是String类重写了equals方法,就可以直接用equals了
if(this.age==anObject.age && this.name.equals(anObject.name)) {
return true;
}
}
return false;
}
}
class Student2{
String name;
int age;
public Student2(String name,int age) {
//有参构造方法
this.name=name;
this.age=age;
}
}
out:
name==name1 :true
name.equals(name1):true
s1==s2 : false
s1.equals(s2) :true
s1==s3 :true
s21==s22:false
s21.equals(s22) :false
s11==s12 :false
s11.equals(s12):true
在源码String类中的equals方法,已经重写了equals方法:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String aString = (String)anObject;
if (coder() == aString.coder()) {
return isLatin1() ? StringLatin1.equals(value, aString.value)
: StringUTF16.equals(value, aString.value);
}
}
return false;
}