You are here: Home / Topics / compareTo() method example in Java

compareTo() method example in Java

Filed under: Java on 2023-08-23 18:49:18

// Program to explain the following String function :
// int compareTo( String str )

class  McompareTo
{
public static void main( String args[ ] )
{
 String s1 = "JAVA";
 String s2 = "JAVA";
 String s3 = "Java";
   
 System.out.println( s1 + " compared to " + s2 + " = " + s1.compareTo( s2 ) );
 System.out.println( s1 + " compared to " + s3 + " = " + s1.compareTo( s3 ) );
}
}


Output:

JAVA compared to JAVA  =  0
JAVA compared to Java  =  -32

About Author:
J
Java     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.