Computer Applications
State the data type and values of a and b after the following segment is executed:
String s1 = "Computer", s2 = "Applications"
a = (s1.compareTo(s2));
b = (s1.equals(s2));
Java String Handling
ICSE 2012
27 Likes
Answer
Data type of a is int and value is 2. Data type of b is boolean and value is false.
Explanation
compareTo() method compares two strings lexicographically and returns the difference between the ASCII values of the first differing characters in the strings. Thus, a will be of int data type and store the value 2 as the difference between ASCII values of 'A' and 'C' is 2.
equals() method compares two String objects and returns true if the strings are same, else it returns false. Thus, b will be of boolean data type and result in false as both the strings are not equal.
Answered By
12 Likes
Related Questions
Define a class to accept the gmail id and check for its validity.
A gmail id is valid only if it has:
→ @
→ .(dot)
→ gmail
→ com
Example:
icse2024@gmail.comis a valid gmail idThe 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); }Write a Java program to enter any sentence and convert the sentence to uppercase. Print only those words of the sentence whose first and last letters are the same.