English

The following code to compare two strings is compiled, the following syntax error was displayed - incompatible types - int cannot be converted to boolean. - Computer Applications

Advertisements
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 an int, not a boolean.

  • It gives:

    • 0 if 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?
2023-2024 (March) Official
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×