java tutorial - Java Strings compareTo() method - java programming - learn java - java basics - java for beginners
Description
- Java Strings compareTo() method in Java is structured in two ways. First, the method compares the string with another object, and the second: the method compares two lines lexically.
- How compareTo() works with a numeric object we covered in the last lesson. Consider the second option: comparing two lines.
Syntax
The syntax of the method is:
Options
Detailed information about the parameters:
- o - object to compare.
- anotherString is a string for comparison.
Return value
- In Java, compareTo() gets the value 0 if the argument is a string lexically equal to this string; the value is less than 0 if the argument is a lexically greater string than the string being compared; and the value is greater than 0 if the argument is a lexically lower string of this string.