public class AAA_1
{
public static void main(String[] args)
{
B<String> b1=new B<String>("absdf");
B<String> b2=new B<String>("absdf");
System.out.println(b1.equals(b2));
int a=300;
B<Integer> b3=new B<Integer>(a);
B<Integer> b4=new B<Integer>(a);
System.out.println(b3.equals(b4));
}
}
class B<L>
{
private L i;
B(L i)
{
this.i=i;
}
public boolean equals(B<L> b)
{
System.out.println(this.i.hashCode()+"\t"+b.i.hashCode());
if(this.i==b.i)
return true;
else
return false;
}
}
seek the guidance of the great God why String is true, and false Integer 's go for it? See online that compares 128 the following is true , I tried like this, that with how the method can be more than 128 figures compare true?
------ Solution ---------------------------------------- ----
put this line into your code
public boolean equals(B<L> b)
{
System.out.println(this.i.hashCode()+"\t"+b.i.hashCode());
//if(this.i==b.i)
if(this.i.equals(b.i))
return true;
else
return false;
}
------ Solution ------------------------------------- -------
you can imagine a == to compare addresses , equals to compare values
------ Solution ------- -------------------------------------
java addition to the basic data types ( eg : int, long, char , etc. ) can be used directly operator == to compare values of variables , other types ( classes, including the outer cover classes ) statement comparing the value of an object or if the content is the same, both must be equals, if = = , the comparison is a reference to an object in memory address value , rather than the object reference content.
------ Solution ---------------------------------------- ----
reportedly 128 to -127 will be generated when the cache Integer object. The next time the same value then the new object, a direct reference to the cache
That 128 to -127 generated object is a
In addition to this once every NEW is a new object.
objects are compared with == number is definitely a different address than the address of the thing that is certainly not equal use equals to compare this.i.equals (bi)
------ For reference only ---------------------------------------
this. i == bi => this.i.equals (b.i)
没有评论:
发表评论