Computer Applications
State the difference between == operator and equals() method.
Java String Handling
ICSE 2016
15 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
7 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.comis a valid gmail idDefine a class to accept a string and convert the same to uppercase, create and display the new string by replacing each vowel by immediate next character and every consonant by the previous character. The other characters remain the same.
Example:
Input : #IMAGINATION@2024
Output : #JLBFJMBSJPM@2024