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);
  }
Answer in Brief

Solution

-4

Reason: compareTo() gives the ASCII difference between the first two dissimilar characters present in the string, but if any one of the strings ends early, then it returns the difference of the length.

shaalaa.com
  Is there an error in this question or solution?
2023-2024 (February) Official
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×
Our website is made possible by ad-free subscriptions or displaying online advertisements to our visitors.
If you don't like ads you can support us by buying an ad-free subscription or please consider supporting us by disabling your ad blocker. Thank you.