what error are you getting?
Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
if(string1.compareTo(string2) == 0)
STRING EQUALS
else
STRINGS DO NOT MATCH
be aware this method can return three values less then zero, zero, or greater then zero. Only equal to zero is string are same
more info on string
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
NEVER use == to compare objects. It doesn't work.
It MIGHT work for Strings, but there's NO guarantee.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
Doesn't == just compare the memory address of two objects to see if its the same? So it'd only return true if the references were the same?
Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
I didn't think you could overload operators like that, as you would do in other languages such as c++?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
I tried a little test, and as far as Strings go, these all return as you would expect if you had used equals().
String s1 = "test1";
String s2 = "test";
boolean test1 = ("test" == "test");
boolean test2 = (s1 == s2);
s2 = "test1";
boolean test3 = (s1 == s2);
System.out.println(test1);
System.out.println(test2);
System.out.println(test3);
Using jdk1.5 if it makes a difference.
Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
I didn't think you could overload operators like that, as you would do in other languages such as c++?
You can't.
Java doesn't feature operator overloading.
The only effective operator overloading is the built in support for string concatenation using the + sign.Doesn't == just compare the memory address of two objects to see if its the same? So it'd only return true if the references were the same?
That is correct.
With the existence of the String object pool however and many people first encountering the operator applied to objects (rather than primitives) when trying to compare Strings they often get confused into thinking it does work to compare the content of objects.
Even with Strings though it can in fact fail, depending on how the Strings were constructed.
String s1 = "Hello World";
String s2 = "Hello World";
boolean b = s1 == s2;
will yield true because "Hello World" will be placed on the String object pool and the JVM will assign s2 to the same actual instance as s1.
String s1 = "Hello World";
String s2 = new String("Hello World");
boolean b = s1 == s2;
will yield false because here you have forced the JVM to create a new String instance on the heap when defining s2 (containing a clone of the content of s1), thus s1 and s2 don't point to the same instance on the String object pool.
And those are simple examples. In real programs it can get a lot more tricky to figure it out, as the 2 Strings might for example come from deserialised objects received from other programs, previous program runs that stored them on disk, or reflection based generation, performed by classes far removed from your comparison to which you may not have the source.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337