This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

String-equals() ang compreTo()  RSS feed

 
selvi parthu
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have tried both the methods for String Object.
Somebody is telling only equals() method for String objects.
and compreTo() for Numbers.Whethre it is true?
please help me.
code:
String s1="A";
String s2="B";
System.out.println(s1.equals(s2));
System.out.println(s1.compreTo(s2));


output:
false
some positive number

if we are using compreTo()method on String object how it will compare two objects.

plz help me.
Thanks in advance.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
selvi, please take a bit more care on selecting in which forums you post your questions. You posted a JSP question in JDBC, and now you have posted a general Java question in the JSP forum.

I've moved this post to Java in General (beginner).
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For details about the compateTo method, look in the API
http://java.sun.com/j2se/1.4.2/docs/api/
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
they're basically the same.
except the syntax you have written down is incorrect.
i believe this is the right one

String s1="A";
String s2="B";
System.out.println(s1.equals(s2));
System.out.println(s1.compareTo(s2)==0);
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody is telling only equals() method for String objects and compreTo() for Numbers.

Don't listen.

When you put objects in a HashMap the map compares keys using equals() Ok, hashcode() and then maybe equals(). The keys could be Strings or Plants or Operas or any Object. When you put objects in a TreeSet the set sorts them using compareTo(). The objects could be Strings or Numbers or Employees or Spacecraft or any Object. You are free to use either method on any Object, too. The methods might not always be implemented in a meaningful way so check the doc or the code to make sure.
[ February 03, 2006: Message edited by: Stan James ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!