Folks, I have a REALLY stupid question to ask. Sorry for asking it. I know my posts are trivial at best, but please, if someone would explain it, that'd be marvellous. I have read this in text books, but the concept just keeps escaping me, as though i can't wrap my head around it. Just what is the difference between =, == and .equals. My interpretation of it is:- We use = only when assigning a value to an identifier, i.e. a value to a variable such as int number = 50. == Errr, well, I THINK this means both sides of an expression give a boolean value of true if they are both equal in VALUE. i.e., a =50, b=50 a == b will return true if a = 50 and b= 60 a == b will return false
.equals - something about objects pointing to the same position in memory, but I haven't the foggiest on that, really. Can the .equals method only be used with Strings? Any help would be greatly appreciated. :roll: [ February 06, 2003: Message edited by: Steve Jensen ]
John Bonham was stronger, but Keith Moon was faster.
Hi Steve, 1. "=" is used for assignment, you got that. 2. "==" is used to compare things. Two cases: a) comparing primitives like your int a = 50, b = 50. "a==b" has a boolean value of true if the "boxes" a and b contain the same value. b) comparing references to objects: if a and b refer to the same object, by same I mean identity then "a==b" is true. Suppose Object a = new ABC() and Object b = a; then "a==b" is true. Suppose Object a = new ABC() and Object b = new ABC(). Then "a==b" is false. Let's make sure you understand those first before moving on to the .equals(Object) thing -Barry [ February 06, 2003: Message edited by: Barry Gaunt ]
If you put line 4 in front of line 3, you will get "Hello Steve" instead of "Hello World" because you made the variable (identifier) point to the String "Steve" instead of pointing to the String "World".
== Errr, well, I THINK this means both sides of an expression give a boolean value of true if they are both equal in VALUE.
== returns true if both things are equal to the same thing. This is true for primitives like int and also for objects if the two objects are the same object. For example, Object a = new Object() ; Object b = a ;
In this case you just told the computer that 'b' is the exact same object as 'a' by assigning 'a' to 'b'. Not only are they equal in value, but 'b' and 'a' are both pointing to the exact same object.
We can say Object c = a ; Now our same object has three names, 'a', 'b', and 'c'.
This is what they mean when they talk about variables (also known as identifiers or sometimes as references) pointing to the same position. Regardless of the name, it's still pointing to the same object. (The object is in the memory)
.equals - something about objects pointing to the same position in memory, but I haven't the foggiest on that, really. Can the .equals method only be used with Strings?
The equals() method can be used with any object, not only with Strings. But, like Barry said, it is important that you understand the first two concepts before we can proceed to this one. [ February 06, 2003: Message edited by: Marilyn de Queiroz ]
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Steve The = sign is used for assignments int a=5; float a=2.01f; String s="Abacus"; String str=s; are all valid statements. So the '=' is used to assign primitives (that is int, boolean, float, double, short, char, long, bye) or objects (String, user defined classes) When used to assign an object, what you are doing is assigning a value to a reference. So in String str=s; you are saying assign the value of the reference s to the reference str. And the value of a reference is the address of that object it is assigned to. '==' returns a boolean if the primitives (int, boolean, char, etc) or references (s, str, variables that point to objects) are equal (that is whether they are equal in value as far as primitives are concerned and whether they point to the same object.) So if int i=20; int j=20; int k=21; String s0="Sam"; String s1="Sam"; String s2=new String("Sam"; then i==j is true i==k is false i==s0 is illegal, incompatible types s0==s1 is true because in effect both s0 and s1 point to the same object because the compiler sees "Sam" already in memory and since new is not used to define s1, it assigns the reference s1 to the same object "Sam, which already has the reference s0. So s0 and s1 point to the same object (same address) s1==s2 is false because the new keyword used in defining s2 causes a completely new reference to be created, and so s1 and s2 do not have the same address values '.equals(Object)' is used to return a boolean that is true if the values of the objects are true, so s0.equals(s1) is true s1.equals(s2) is true because all the objects have the value "Sam" So in summary '=' is used for assignment == is used to compare values of primitives or references .equals() is used to compare values of objects Hope its clear
May the force of the Java be in all of us !!!
Joined: Sep 23, 2002
So far so good Cheers folks, all of you, who took the trouble to reply. Guess i'll just put the .equals on the back burner for a bit, then.
The equals method has a default behavior inherited from the Object class. The default behavior is to do exactly what the == does. Any method can override the equals method if they choose to. The String class, for example, has overridden the equals so that you can compare the contents of two Strings.
My first programming teacher taught us to read code out loud in normal speech. A = 7 That's "A receives the value 7" A == 7 That�s "A is exactly 7"
Marilyn de Queiroz
Joined: Jul 22, 2000
Any class can override the equals method if they choose to. The String class, for example, has overridden equals() so that you can compare the contents of two Strings.
The equals() method is a little more complex than '=' or "==".
As Thomas said, the default equals() method which is found in the Object class is identical to "==". In other words, object1.equals( object2 ) will tell you if object1 and object2 are really the exact same object. For example, if my address is 123 MyStreet and you lived in the same house with me, I could say Object myAddress = "123 MyStreet" ; Object yourAddress = myAddress ;
Now myAddress and yourAddress point to the same house (the same Object). In this case, myAddress == yourAddress is true. myAddress.equals( yourAddress ) is also true.
However, some classes override the equals() method so that equals() will return true if they contain equivalent stuff, even if they are not the exact same object. This is true for many of the classes in the basic java classes, like the String class for example. However, if you create a new class (like class Address, for example), it would need to contain an equals() method which overrides the equals() method of Object if you wanted people to be able to compare Addresses of equal value. Otherwise equals() would only tell you if it were the exact same Address object.
Integer twenty = new Integer(20); Integer twentey = new Integer(20);
This is because the Integer class has overridden the equals() method so that two Integers with the same value will be "equal" even though they are not the same Integer object (because you used "new" twice).
Now if you assign twentey = twenty ; you will see that twentey and twenty are now the exact same object, so twenty == twentey returns true and twenty.equals( twentey ) also returns true.
Clear as mud? [ February 13, 2003: Message edited by: Marilyn de Queiroz ]