Difference Between Comparing String Using == and String.equals() Method in Java8 Oct 2025 | 4 min read In Java, a strixng is an object, and comparing objects requires considering whether you want to compare their references (memory addresses) or their values they have. Java provides the following two ways to compare strings:
Using Equals Operator (==)The equals operator (==) compares the references (memory addresses) of the string objects rather than their contents. It determines whether two string objects refer to the same memory location. It returns true if the references are identical, meaning they point to the same memory address, and false if they do not. It is primarily used to compare primitive types and object references. Example: Equals Operator (==)ExampleCompile and RunOutput: true false Using String.equals() MethodThe String.equals() method overrides the equals() method of the Object class. It compares the content (values) of two objects. The method returns true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object. Example: String.equals() MethodExampleCompile and RunOutput: true true false false Equals Operator (==) Vs. String.equals() Method
Important Points to Remember
String.equals() and == Operator MCQs1. For comparing address of any two strings, we use ___________.
Answer: B Explanation: When used to compare strings, the equality operator (==) compares the references (memory addresses) of the string objects rather than their contents. 2. For comparing content of any two strings, we use ___________.
Answer: A Explanation: The String.equals() compares the content (values) of two objects for equality. The primary purpose of the String.equals() method is to determine if two objects are meaningfully equivalent based on their internal state. 3. Which of the following methods returns a Boolean value?
Answer: A Explanation: The String.equals() compares the content (values) of two objects for equality, and returns a Boolean value. The String.compareTo() method returns an integer value. Also, == is an operator, not a method. 4. Predict the output of the following code snippet.
Answer: B Explanation: In the above code snippet, two new objects are created. Therefore, both the strings will have different addresses. When the comparison between these two strings is made using the equality operator in the print statement, a false value is returned. 5. Predict the output of the following code snippet.
Answer: C Explanation: The String.equals() compares the content of the string. Since the contents of both the strings are equal, a true value is returned. |
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India