Computer Applications
Which of the following returns a value greater than or equal to 0?
Java String Handling
3 Likes
Answer
length()
Reason — Analyzing the given options:
1. lastIndexOf(char ch)
:
- Returns the last index of the specified character in the string.
- If the character is not found, it returns
-1
, which can be less than0
. - Not always greater than or equal to 0.
2. length()
:
- Returns the number of characters in the string.
- This is always greater than or equal to 0, as an empty string has a length of
0
.
3. compareTo(String s)
:
- Compares two strings lexicographically.
- Can return negative, 0, or positive values, depending on the comparison result.
- Not always greater than or equal to 0.
4. equalsIgnoreCase(String s)
:
- Returns a boolean value (
true
orfalse
), not a numeric value.
Answered By
3 Likes
Related Questions
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.
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.com
is a valid gmail idTwo strings,
city1
andcity2
, are compared usingcity1.compareTo(city2)
, and the result is less than zero. What does this indicate?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); }