944,117 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3139
  • Java RSS
Nov 30th, 2006
0

hi.. string compare

Expand Post »
hi. this is harisha, newbie.

how to compare two strings in java. .

String id,t;

if (id.compareTo(t) > 2)

{....}

is this correct.while i compile it shows error.


reply me soon..
Last edited by harisha; Nov 30th, 2006 at 5:47 am. Reason: urgent
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
harisha is offline Offline
1 posts
since Nov 2006
Dec 1st, 2006
0

Re: hi.. string compare

what error are you getting?
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Dec 1st, 2006
0

Re: hi.. string compare

Java Syntax (Toggle Plain Text)
  1. if(string1.compareTo(string2) == 0)
  2. STRING EQUALS
  3. else
  4. 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
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Dec 1st, 2006
0

Re: hi.. string compare

You can use equals() method or == operator to compare two Strings instead of compareTo() method.
Reputation Points: 10
Solved Threads: 17
Junior Poster
java_programmer is offline Offline
118 posts
since May 2006
Dec 1st, 2006
0

Re: hi.. string compare

NEVER use == to compare objects. It doesn't work.
It MIGHT work for Strings, but there's NO guarantee.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 1st, 2006
0

Re: hi.. string compare

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?
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Dec 1st, 2006
0

Re: hi.. string compare

I didn't think you could overload operators like that, as you would do in other languages such as c++?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Dec 1st, 2006
0

Re: hi.. string compare

I tried a little test, and as far as Strings go, these all return as you would expect if you had used equals().

Java Syntax (Toggle Plain Text)
  1. String s1 = "test1";
  2. String s2 = "test";
  3.  
  4. boolean test1 = ("test" == "test");
  5. boolean test2 = (s1 == s2);
  6. s2 = "test1";
  7. boolean test3 = (s1 == s2);
  8.  
  9. System.out.println(test1);
  10. System.out.println(test2);
  11. System.out.println(test3);

Using jdk1.5 if it makes a difference.
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Dec 2nd, 2006
0

Re: hi.. string compare

Click to Expand / Collapse  Quote originally posted by iamthwee ...
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.

Quote ...
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.

Java Syntax (Toggle Plain Text)
  1. String s1 = "Hello World";
  2. String s2 = "Hello World";
  3. 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.

Java Syntax (Toggle Plain Text)
  1. String s1 = "Hello World";
  2. String s2 = new String("Hello World");
  3. 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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Anyone mind helping out a stuck noob?
Next Thread in Java Forum Timeline: JDialog components are not visible





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC