|
-
Dec 4th, 2005, 05:04 PM
#1
String.equals()
Why is it, that if I need to check equality between two strings I have to use String.equals() rather than ==?
-
Dec 4th, 2005, 06:23 PM
#2
New Member
Re: String.equals()
This is because strings are objects. An object is an instance of a class, in other words your String variable is a reference to a String object. If you use the == operator you are comparing the reference value rather than the object itself.
-
Dec 5th, 2005, 01:40 AM
#3
Re: String.equals()
I thought equals was one of those special methods link toString() which is invoked when two objects are compared?
-
Dec 5th, 2005, 11:07 AM
#4
Re: String.equals()
Java does not support operator overloading, so AFAIK the == operators will always compare object instances, whereas a specific .equals method can do what it likes (in this case compare string content). In .NET it is the other way round, the string class overloads the == operator for string comparison and .Equals executes a reference comparison.
-
Dec 5th, 2005, 11:24 AM
#5
Re: String.equals()
Which, IMHO, is quite braindead of C#. (Read: unintuitive) But I'm sure some people will disagree.
== always compares the variable values in Java, no exceptions. And since every variable but the primitives is of reference type, it's always the reference value that's compared for objects.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 5th, 2005, 11:35 AM
#6
Re: String.equals()
Thanks for the help. I just assumed that it be like the toString() method because all objects have an equals method. Out of interest would a comparison here be that of primitive types?
Code:
if ("String1" == "String2")
Or would that again be a comparison between two references to String objects?
-
Dec 5th, 2005, 11:39 AM
#7
Re: String.equals()
String literals in Java are String objects, so the latter I'd assume.
-
Dec 5th, 2005, 12:40 PM
#8
Re: String.equals()
It would be objects. On the other hand, string literals are, I believe, interned, so identical string literals are actually the same object. At least in Sun's JVM.
Also, toString() isn't as magical as you think. You may think that a string concatenation automatically generates toString() calls, but it really doesn't. This:
Code:
"The current date and time is " + new Date() + "."
is translated to this (JDK 1.5):
Code:
new StringBuilder().append("The current date and time is ").append(new Date()).append(".")
The "magic" of this comes from the overloaded append() method, which has the obvious String one and one for Object, that works like so:
Code:
public StringBuilder append(Object o) {
return append(o.toString());
}
Other overloads are provided for the primitives.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|