The following code to compare two strings is compiled, the following syntax error was displayed - incompatible types - int cannot be converted to boolean.
Advertisements
प्रश्न
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);
}अति संक्षिप्त उत्तर
Advertisements
उत्तर
−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
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
