compareTojava
The compareTo() method in Java is used for comparing two objects. It is defined in the Comparable interface and is implemented by various classes in Java. The method returns an integer value that represents the result of the comparison.
The compareTo() method compares two objects based on their natural ordering. For example, the String class implements the compareTo() method to compare two strings lexicographically.
The general syntax of the compareTo() method is:
public int compareTo(Object obj)
The method takes an object as an argument and returns an integer value. The return value is negative if the object is less than the argument object, zero if they are equal, and positive if the object is greater than the argument object.
For example, consider the following code that compares two strings using the compareTo() method:
String str1 = "apple"; String str2 = "banana";
int result = str1.compareTo(str2);
System.out.println(result);
In this code, the result variable will contain a negative value because "apple" comes before "banana" in the dictionary
原文地址: http://www.cveoy.top/t/topic/cn9V 著作权归作者所有。请勿转载和采集!