๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ system.string.compareto
String.CompareTo Method (System) | Microsoft Learn
Else Console.WriteLine("Generic CompareTo = {0}; non-generic CompareTo = {1}", _ resultGeneric, resultNonGeneric) End If End Sub End Class ' 'This example produces the following results: ' 'The following is the result of using the generic and non-generic versions of the 'CompareTo method for several base types: ' 'Boolean: True is equal to True 'Byte: 1 is equal to 1 'Int16: -2 is less than 2 'Int32: 3 is equal to 3 'Int64: 4 is greater than -4 'Decimal: -5.5 is less than 5.5 'Single: 6.6 is equal to 6.6 'Double: 7.7 is greater than -7.7 'Char: A is equal to A 'String: abc is equal to abc 'Dat
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_string_compareto.asp
Java String compareTo() Method
A value less than 0 is returned if the string is less than the other string (less characters) and a value greater than 0 if the string is greater than the other string (more characters). Tip: Use compareToIgnoreCase() to compare two strings lexicographyically, ignoring lower case and upper ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-string-compareto-method-with-examples
Java String compareTo() Method with Examples - GeeksforGeeks
January 20, 2025 - There are three variants of the compareTo() method which are as follows: ... This method compares this String to another Object.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2013 โ€บ 12 โ€บ java-string-compareto-method-example
Java String compareTo() Method with examples
This method takes string as an argument and returns an integer. It returns positive number, negative number or zero based on the comparison: str1.compareTo(str2) returns positive number, if str1 > str2
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ java/lang โ€บ java string compareto method
Java String compareTo Method
September 1, 2008 - If the string is equal to the string argument, but the argument string is in a different case, the compareTo() method returns the positive value. In the following example, we are instantiating the string class with the value "hello" and "HELLO".
๐ŸŒ
Javatpoint
javatpoint.com โ€บ java-string-compareto
String compareTo()
Java String compareTo() method with method signature and examples of concat, compare, touppercase, tolowercase, trim, length, equals, split, string compareto in java etc.
๐ŸŒ
Reddit
reddit.com โ€บ r/javahelp โ€บ understanding the compareto() method
r/javahelp on Reddit: Understanding the compareTo() method
August 8, 2020 -

From what I have been reading, the compareTo() method returns the difference of the Unicode numerical values of two Strings when they are compared with each other. For instance, the String "hello" when compared with the String "hello" returns an integer value of zero, since they both have exactly the same Unicode characters in them. Based on my understanding of this method, "hello" should return zero when compared to "olleh", because the two Strings have the exact same Unicode characters in them. Instead, though, I am getting integer value of 7 returned to the console. Can someone break this down a bit for me to help me understand it better? Thanks in advance. Here is my code:

String str1 = "hello";
String str2 = "olleh";
System.out.println(str1.compareTo(str2)); // 7

๐ŸŒ
Guru99
guru99.com โ€บ home โ€บ java tutorials โ€บ java string manipulation with example
Java String Manipulation with EXAMPLE
November 26, 2024 - public class Sample_String{ public static void main(String[] args){//Compare to a String String str_Sample = "RockStar"; System.out.println("Compare To 'ROCKSTAR': " + str_Sample.compareTo("rockstar")); //Compare to - Ignore case System.out...
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ java string compareto() examples
Java String compareTo() examples - Mkyong.com
January 24, 2022 - The "a" is lexicographically precedes the argument string "c"; it returns a negative integer. In simple words, for lexicographical or alphabetical order, "a" comes before the "c" and returns a negative integer. System.out.println("a".compareTo("b")); // -1 System.out.println("a".compareTo("c")); // -2 System.out.println("a".compareTo("d")); // -3 System.out.println("a".compareTo("e")); // -4 System.out.println("1".compareTo("2")); // -1 System.out.println("1".compareTo("3")); // -2 System.out.println("1".compareTo("4")); // -3 System.out.println("1".compareTo("5")); // -4
Find elsewhere
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ library โ€บ string โ€บ compareto
Java String compareTo()
The compareTo() method takes the letter case (uppercase and lowercase) into consideration. class Main { public static void main(String[] args) { String str1 = "Learn Java"; String str2 = "learn Java"; int result; // comparing str1 with str2
๐ŸŒ
Software Testing Help
softwaretestinghelp.com โ€บ home โ€บ java โ€บ java string compareto method with programming examples
Java String compareTo Method With Programming Examples
April 1, 2025 - In the above example, we have taken two Strings that have the same value keeping one String in Uppercase and another one in Lowercase. Now, a Java .compareTo() method will provide results based on the ASCII difference in the value of the Lowercase ...
๐ŸŒ
Particle
docs.particle.io โ€บ reference โ€บ device-os โ€บ api โ€บ string-class โ€บ compareto
compareTo() - String class | Reference | Particle
That means, for example, that 'a' comes before 'b' but after 'A'. Numbers come before letters. String comparison only compares the 8-bit ASCII values. It does not compare UTF-8 encoded strings properly. // SYNTAX string.compareTo(string2) // PROTOTYPE int compareTo(const String &s) const;
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ java string compareto() method
Java String compareTo() Method with Examples - Scaler Topics
May 10, 2023 - In the example above, when we compare an empty string with a non-empty string it returns a negative number which is nothing but the length of the non-empty string. Vice-versa, when we compare the non-empty string with the empty one, we get a positive number that is again the length of the non-empty string. Thus, using the compareTo() in Java we can find the length of the string by comparing with an empty string as it returns the length of the non-empty string with positive and negative signs as per the position of the non-empty string.
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ strings in java โ€บ java string compareto() method
Java String CompareTo() Method
December 25, 2024 - Role: The parameter represents the string to be compared with the string object that invokes the method. Case Sensitivity: The comparison is case-sensitive, meaning that uppercase and lowercase characters are treated differently. If the parameter is null, a NullPointerException is thrown. Always ensure that the parameter is not null. The compareTo() method compares strings based on the Unicode value of each character.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ java โ€บ strings โ€บ .compareto()
Java | Strings | .compareTo() | Codecademy
July 14, 2025 - Yes, it is. Uppercase letters have lower Unicode values than lowercase letters. For example, "Apple".compareTo("apple") returns a negative number. The .compareToIgnoreCase() method compares two strings lexicographically but ignores case differences.
๐ŸŒ
TechVidvan
techvidvan.com โ€บ tutorials โ€บ java-string-compareto-method
Java String compareTo() Method with Examples - TechVidvan
March 7, 2024 - Lexicographic Comparison: You can compare strings lexicographically (in a dictionary-style) with the compareTo() function.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ system.string.compare
String.Compare Method (System) | Microsoft Learn
Imports System.Globalization Module ... End Sub End Module ' The example displays the following output: ' Comparison of 'ani-mal' and 'animal': 0 ยท To recognize ignorable characters in a string comparison, call the ...
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ comparing strings in java
Comparing Strings in Java | Baeldung
June 19, 2024 - Letโ€™s see an example: String author = "author"; String book = "book"; String duplicateBook = "book"; assertThat(author.compareTo(book)) .isEqualTo(-1); assertThat(book.compareTo(author)) .isEqualTo(1); assertThat(duplicateBook.compareTo(book))
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ standard โ€บ base-types โ€บ comparing
Comparing Strings in .NET - .NET | Microsoft Learn
Dim string1 As String = "Hello World" Dim string2 As String = "Hello World!" Dim MyInt As Integer = string1.CompareTo(string2) Console.WriteLine(MyInt) This example displays -1 to the console.
๐ŸŒ
Tpoint Tech
tpointtech.com โ€บ java-string-compareto
Java String compareTo() Method - Tpoint Tech
March 17, 2025 - String compareTo() String concat() String contains() String endsWith() String equals() equalsIgnoreCase() String format() String getBytes() String getChars() String indexOf() String intern() String isEmpty() String join() String lastIndexOf() String length() String replace() String replaceAll() String split() String startsWith() String valueOf() String substring() String toCharArray() String toLowerCase() String toUpperCase() String trim() Java Array Class ยท