Home > Back-end >  Object class to rewrite the problem
Object class to rewrite the problem

Time:10-19

 public class Eas {
private int age;
Private int a;

Public Eas (int the age, int a) {
Enclosing the age=age;
Enclosing a=a;
}

Public Eas () {
}

Public int getAge () {
Return the age;
}

Public void setAge (int age) {
Enclosing the age=age;
}

Public int getA () {
Return a;
}

Public void setA (int a) {
Enclosing a=a;
}

@ Override
Public Boolean equals (Object o) {
If (this==o) return true;
If (o==null | | getClass ()!=o.g etClass ()); return false
Eas Eas=(Eas) o;
Return the age==eas. Age & amp; &
A==eas. A;
}

@ Override
Public int hashCode () {
Return Objects. The hash (age, a);
}
}

An Eas custom classes, when running
 
Package teste.

Public class m {
Public static void main (String [] args) {
Eas Eas=new Eas (12, 1);
Eas eas1=new Eas (12, 1);
System. The out. Println (eas);

System. The out. Println (eas1);
System. The out. Println (eas==eas1);




;
}
}

Output the first two are the same, the third output is false, excuse me somebody know why two output value is the same as before? Did not rewrite the toString method in the class? The output address should be different, the third output==false indicates the two objects is not the same as the address, here is how to return a responsibility excuse me? There are a great god know?

CodePudding user response:

The==comparison is whether two variables point to the same object instance in memory, namely they two memory address is the same, if you want to compare two logically the same, whether should use the equals

CodePudding user response:

Output is not the address, the output value has relationship with hashcode, details see the source code, because you override the hashcode method, all of the two objects hashcode, you cancel the rewrite the can see different results

CodePudding user response:

Because the==is memory address values of contrast.

System. The out. Println (System. IdentityHashCode (eas));
System. The out. Println (System. IdentityHashCode (eas1));

Print once you know the result!

CodePudding user response:

All bosses said I watched, I know it's the==comparison on the object's address,
Firstly the two objects must be two objects, the address value must be different!
 System. Out. Println (eas); 

System. Out.println (eas1)

But the two words of the output value should not be the result of address? Don't rewrite the hashCode methods will make the output value change into other values? What the value is that again?
  • Related