The following code to compare two strings is compiled, the following syntax error was displayed - incompatible types - int cannot be converted to boolean.
Advertisements
Question
The following code to compare two strings is compiled, the following syntax error was displayed - incompatible types - int cannot be converted to boolean.
Identify the statement which has the error and write the correct statement. Give the output of the program segment.
void calculate()
{
String a="KING",b="KINGDOM";
boolean x=a.compareTo(b);
system.out.println(x);
}Very Short Answer
Advertisements
Solution
−3
Explanation:
-
The method
compareTo(String str)in Java returns anint, not aboolean. -
It gives:
-
0if both strings are equal -
A positive number if
a > b -
A negative number if
a < b
-
shaalaa.com
Is there an error in this question or solution?
