| | |
hi.. string compare
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Java Syntax (Toggle Plain Text)
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
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Mar 2004
Posts: 765
Reputation:
Solved Threads: 38
I tried a little test, and as far as Strings go, these all return as you would expect if you had used equals().
Using jdk1.5 if it makes a difference.
Java Syntax (Toggle Plain Text)
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.
•
•
•
•
I didn't think you could overload operators like that, as you would do in other languages such as c++?
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?
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.
Java Syntax (Toggle Plain Text)
String s1 = "Hello World"; String s2 = "Hello World"; boolean b = s1 == s2;
Java Syntax (Toggle Plain Text)
String s1 = "Hello World"; String s2 = new String("Hello World"); boolean b = s1 == s2;
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
![]() |
Similar Threads
- to convert string to integer value (Legacy and Other Languages)
- Compare String with Strings (Visual Basic 4 / 5 / 6)
- string: get token from string and compare token from text file (C++)
- How to see if one string is less than another string (C)
- string compare and fstream (C)
- need info? can't use strcmp/string compare (C++)
Other Threads in the Java Forum
- Previous Thread: Anyone mind helping out a stuck noob?
- Next Thread: JDialog components are not visible
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary bluetooth character chat class classes client code component consumer csv database desktop draw eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop map method methods mobile netbeans newbie objects online oracle oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion robot rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time tree ubuntu update windows




. 

