Computer Applications
Differentiate between equals() and ==
Java String Handling
ICSE 2008
39 Likes
Answer
equals() | == |
---|---|
It is a method | It is a relational operator |
It is used to check if the contents of two strings are same or not | It is used to check if two variables refer to the same object in memory |
Example: String s1 = new String("hello"); String s2 = new String("hello"); boolean res = s1.equals(s2); System.out.println(res); The output of this code snippet is true as contents of s1 and s2 are the same. | Example: String s1 = new String("hello"); String s2 = new String("hello"); boolean res = s1 == s2; System.out.println(res); The output of this code snippet is false as s1 and s2 point to different String objects. |
Answered By
25 Likes
Related Questions
Consider the following String array and give the output
String arr[]= {"DELHI", "CHENNAI", "MUMBAI", "LUCKNOW", "JAIPUR"}; System.out.println(arr[0].length() > arr[3].length()); System.out.print(arr[4].substring(0,3));
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 compareTo() and equals()
Differentiate between toLowerCase() and toUpperCase()