hi.. string compare

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2006
Posts: 1
Reputation: harisha is an unknown quantity at this point 
Solved Threads: 0
harisha harisha is offline Offline
Newbie Poster

hi.. string compare

 
0
  #1
Nov 30th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 765
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: hi.. string compare

 
0
  #2
Dec 1st, 2006
what error are you getting?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,217
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 489
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: hi.. string compare

 
0
  #3
Dec 1st, 2006
  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
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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 21
Reputation: java_programmer is an unknown quantity at this point 
Solved Threads: 0
java_programmer java_programmer is offline Offline
Newbie Poster

Re: hi.. string compare

 
0
  #4
Dec 1st, 2006
You can use equals() method or == operator to compare two Strings instead of compareTo() method.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: hi.. string compare

 
0
  #5
Dec 1st, 2006
NEVER use == to compare objects. It doesn't work.
It MIGHT work for Strings, but there's NO guarantee.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 765
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: hi.. string compare

 
0
  #6
Dec 1st, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: hi.. string compare

 
0
  #7
Dec 1st, 2006
I didn't think you could overload operators like that, as you would do in other languages such as c++?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 765
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: hi.. string compare

 
0
  #8
Dec 1st, 2006
I tried a little test, and as far as Strings go, these all return as you would expect if you had used equals().

  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: hi.. string compare

 
0
  #9
Dec 2nd, 2006
Originally Posted by iamthwee View Post
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.

  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.

  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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC