The == operator determines if 2 references point to the same object.
The == operator determines if 2 references point to the same object. So Object o1 = new Object(); Object o2 = o1; o1 == o2; //true o2 = new Object(); o1 == o2 // false the Object.equals() method is "how do I determine if 2 references to objects, that are not the same object, are equal? " If two references point to the same object, both o1 == o2 o1.
Equals(o2) should be true. But if o1 and o2 are not the same object, they still might be equal logically. For any given class, equals depends on the semantics behind the object.
For example, consider a class where field1 and field2 are set by the user, but field3 is computed and has a random element to its computation. It might make sense to define equals in this case to only depend on field1 and field2, and not field3. Thats why equals is necessary.
– masato-san Dec 22 '10 at 2:05 2 hashcode is used by the various hash based collections, like HashSets.It is your decision whether to use hashcode in equals, but be aware that it is not always the case that if 2 objects return the same hashcode, they are equal. I wouldn't. – hvgotcodes Dec 22 '10 at 2:13.
If you don't already have a copy; buy a copy of Effective Java by Joshua Bloch. This is the de facto reference for Java developers and has a lot of information on this (and many other) subject.
Going to book store this weekend :D that would be my xmas present! – masato-san Dec 23 '10 at 23:36.
(used on objects rather than on primitive values) tests whether 2 objects are actually the same object; it compares whether the pointers are actually pointing to the same memory location. .equals() is defined by the object itself. String s1 = new String(" String s2 = new String(" boolean b1 = ( s1 == s2 ) ; // false: s1 and s2 point to different objects boolean b2 = ( s1.
Equals(s2) ) ; // true: s1 and s2 both represent the same // piece of text - "hashCode() is an optimisation trick (in most of its uses, anyway). A lot of code in the standard libraries makes the assumption that if o1. Equals(o2)==true then o1.hashCode()==o2.hashCode() and that if o1.hashCode()!
=o2.hashCode() then o1. Equals(o2)==false in order to work faster. The most obvious example of such an optimisation is the HashMap class.
This makes retrieving objects using a key really fast, but breaks badly if hashCode and equals don't work properly for the key elements. In fact, this is one of the reasons that the String class is immutable: if you were able to modify a String (and so change its hashCode) while that String was the key in a HashMap, then you would never be able to find it, since you would end up looking for it in the wrong place! Other answers recommend Effective Java by Joshua Bloch.
If you are asking such questions, then now is the perfect time in your career to buy the book and read it cover to cover. It'll also be worth re-reading it in a year or two's time, when you'll have forgotten some of it and more of it will make sense...
Is identity. .equals() is equality. .equals() defaults to just using == (just like hashCode() defaults to System.
IdentityHashCode() but you can override them if there's a more meaningful way to check for equality. Typically this is a sort of "structural" equality. Ie: are all of the pieces of this .equal() to all of the pieces of that?
Most is already answered, so here's just another enlightening example: String s1 = "foo"; String s2 = "foo"; System.out. Println(s1 == s2); // true, because same reference (string pool) String s3 = new String("foo"); String s4 = new String("foo"); System.out. Println(s3 == s4); // false, because different reference System.out.
Println(s3. Equals(s4)); // true, because same value.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.