• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String-equals() ang compreTo()

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic