import java.lang.String.*;
class myclass_compareto
public void process(String s1, String s2)
System.out.println("You entered : " + s1 + ":" + s2 );
int n = s1.compareTo(s2);
System.out.println("--Compare frist with second---");
if (n==0){ System.out.println("Matched ");}
else if(n>0){System.out.println("Did not Match: Value is more ");}
else if(n<0){System.out.println("Did not Match: Value is less ");}
System.out.println("--Compare second with first---");
int n2 = s2.compareTo(s1);
if (n==0){ System.out.println("Matched ");}
else if(n2>0){System.out.println("Did not Match: Value is more ");}
else if(n2<0){System.out.println("Did not Match: Value is less ");}
public class test_compareto
public static void main(String arguments[])throws IOException
InputStreamReader isr = new InputStreamReader(;
BufferedReader br = new BufferedReader(isr);
//needs throws IOException
System.out.print("Type some thing : ");
String str1 =br.readLine();
System.out.print("Type the same : ");
String str2 =br.readLine();
myclass_compareto my = new myclass_compareto();
my.process(str1, str2);
Here's the right way to do it:
You should use one of the following tests to compare the contents of two strings:
if (str1.equals(str2))
if (str1.compareTo(str2) < 0)
if (str1.equalsIgnoreCase(str2))
Any of these statements compare the contents of the two strings name str1 and str2. There are other ways to compare the contents of two Strings, but these are the most-frequently used methods I'm aware of.
Here's the wrong way to do it:
Many times we make the mistake of using the following syntax when comparing strings:
if (str1 == str2)
A comparison of objects (such as Strings) using the == operator doesn't compare the contents of the Strings. Instead, it compares the address of the two Strings.