Computer Applications
Differentiate between compareTo() and equals()
Java String Handling
ICSE 2006
42 Likes
Answer
compareTo() | equals() |
---|---|
It compares two strings lexicographically. | It checks if contents of two strings are same or not. |
The result is a negative, positive or zero integer value depending on whether the String object precedes, follows or is equal to the String argument | The result is true if the contents are same otherwise it is false. |
Answered By
26 Likes
Related Questions
Your friend is trying to write a program to find and display the frequency of vowels in a string. Due to confusion in the syntax of some of the statements, he could not complete the program and has left some places blank marked with ?1?, ?2?, ?3? and ?4? to be filled with expression/function. The incomplete program is shown below:
import java.util.*; class Vowels { public static void main(String args[]) { Scanner ...?1?... = new Scanner(System.in); String st; int i, b, v = 0; char chr; System.out.println("Enter a string"); st = ............?2?............; // to input a string st = ............?3?...........; // to convert the string into uppercase b = st.length(); for( i = 0 ; i < b ; i++) { chr = ............?4?............; // to extract a character if(chr == 'A' || chr == 'E' || chr == 'I' || chr == 'O' || chr == 'U') v = v + 1; } System.out.println("No. of Vowels = " + v); } }
Based on the above discussion, answer the following questions:
(a) What expression/function will be filled in place of ?1?
(b) What expression/function will be filled in place of ?2?
(c) What expression/function will be filled in place of ?3?
(d) What expression/function will be filled in place of ?4?
Differentiate between equals() and ==
Differentiate between toLowerCase() and toUpperCase()
Differentiate between charAt() and substring()