The Java String compareTo() method is used for comparing two strings lexicographically. Each character of both the strings is converted into a Unicode value for comparison. 1. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. Java String compareTo() is an inbuilt method that is used to compare two strings lexicographically where each character of both the strings are converted into a Unicode value. String comparison. We can compare string in java on the basis of content and reference. The Comparable interface defines only this single method. In the Java API on oracles website: "compareTo Returns: "the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument." As the name suggests, it compares two given Strings and finds out if they are the same or which one is greater. The String class represents character strings. The String.compareTo() method we used above is derived from the java.lang.Comparable interface, which is implemented by the String class. The java.lang.String.compareTo() method compares two strings lexicographically.The comparison is based on the Unicode value of each character in the strings. It is used in authentication (by equals() method), sorting (by compareTo() method), reference matching (by == operator) etc.. Java String compareTo() Method. Description. The compareTo() method compares the Unicode value of each character in the two strings you are comparing. The Java Comparable Interface. この記事ではString型の大小を比較する方法をわかりやすく解説します! 文字列の大小を比較したい compareの使い方を知りたい compareToの戻り値の数値の意味を知りたい compareとequalsの違いを知りたい 今回はそんな悩みを解決する文字列の大小の比較についてです。 All classes, whose objects should be … The Java string compareTo() method is used to compare two strings lexicographically. Method Returns: The Java String compareTo() method is defined in interface java.lang.Comparable . Java String compare. Java String compareTo() method compares two strings lexicographically. The Java String compareTo() method is used to check whether two Strings are identical or not. The return type of Java compareTo() method is an integer and the syntax is given as: int compareTo(String str) There are three ways to compare string in java: String buffers support mutable strings. java.lang.Object java.lang.String 所有已实现的接口: Serializable, CharSequence, Comparablepublic final class String extends Object implements Serializable, Comparable < String >, CharSequence If both the strings are equal then this method returns 0 else it returns positive or negative value. All string literals in Java programs, such as "abc", are implemented as instances of this class.. Strings are constant; their values cannot be changed after they are created. We can consider it dictionary based comparison. 在此,我总结下关于String类中的compareTo方法,以备应对以后的笔试面试。 String类的定义: java.lang 类 String. "Here is an if statement: Each character of both the strings is converted into the Unicode value for comparison. Syntax: How to write a compareTo() method in Java: public int compareTo(String str) Parameter input : str – The compareTo() function in Java accepts only one input String data type. Java String compareTo() method is used for comparing the two strings lexicographically. If a string 'str1' comes before another string 'str2' in dictionary, then str2 is said to be greater than 'str1' in string comparison.. string1 > string2 – ‘string1’ comes AFTER ‘string2’ in dictionary.