what is the resulting value of the expression “sears”.compareto(“chicago”)? course hero

by Garth Jones 4 min read

What is compareTo () method in Java?

compareTo () is used for comparing two strings lexicographically. Each character of both strings are converted into a Unicode value. However, if both the strings are equal, then this method returns 0 else it only result either negative or positive value.

What character comes after a alphabetically?

Character b comes after a alphabetically. Hence output is 1

What happens if the first string is always higher than the second string?

In this method, if the first string is always lexicographically higher than second string, it returns a positive number.

What is the Java String CompareTo method?

Java String compareTo () method is used to perform natural sorting on string. Natural sorting means the sort order which applies on the object, e.g., lexical order for String, numeric order for Sorting integers, etc.

What does compareTo do?

On comparing these two string, if the outcome is 0, then the compareTo () method does not consider the case sensitiveness of characters; otherwise, the method considers the case sensitiveness of characters.

What happens if the first string is greater than the second string?

If the first string is lexicographically greater than the second string, it returns a positive number (difference of character value). If the first string is less than the second string lexicographically, it returns a negative number, and if the first string is lexicographically equal to the second string, it returns 0.

What happens if you compare a string with a blank string?

If you compare string with blank or empty string, it returns length of the string. If second string is empty, result would be positive. If first string is empty, result would be negative.