11 May String compareTo(Object o) method in Java
The compareTo(Object o) method in Java compares the String to another Object.
Syntax
Let us see the syntax,
1 2 3 |
int compareTo(Object o); |
Parameters
Let us see the parameters,
- O – object
Example
The following is an example of compareTo(Object o),
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class StudyopediaDemo { public static void main(String args[]) { int value; String msg1 = "Welcome to Australia!"; String msg2 = new String("Welcome to Australia!"); System.out.println("String one: " +msg1); System.out.println("String two: "+msg2); value = msg1.compareTo( msg2 ); System.out.println(value); } } |
Output
The following is the output,
No Comments