49.1k views
2 votes
String w1;

String w2 = "apple";
String w3 = "banana";

What is the output?

System.out.println(w3.compareTo("fruit"));

User Eawer
by
8.2k points

1 Answer

1 vote

Final answer:

The output of the code will be -4.

Step-by-step explanation:

The output of the code will be -4.

To method in Java is used to compare two strings lexicographically. It returns an integer value that represents the difference between the two strings.

In this case, w3.compareTo("fruit") compares the string "banana" with "fruit". Since "banana" comes before "fruit" in lexicographic order, the returned value is a negative integer.

The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The method returns 0 if the string is equal to the other string.

To convert string to int, we use the int() method in python. To convert a string into a floating-point number, we use the float() method in python. The int in python is a built-in method that convert string to int. The int() method can convert a string or a number into an integer.

User Janadari Ekanayaka
by
8.5k points