- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between equals() method and == operator in java?
The equals() method compares this string to the specified object. The result is 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
public class Sample{ public static void main(String []args){ String s1 = "tutorialspoint"; String s2 = "tutorialspoint"; String s3 = new String ("Tutorials Point"); System.out.println(s1.equals(s2)); System.out.println(s2.equals(s3)); } }
Output
true false
You can also compare two strings using == operator. But, it compares references of the given variables not values.
Example
public class Sample{ public static void main(String []args){ String s1 = "tutorialspoint"; String s2 = "tutorialspoint"; String s3 = new String ("Tutorials Point"); System.out.println(s1 == s2); System.out.println(s2 == s3); } }
Output
true false
- Related Articles
- Difference between == and equals() method in Java.
- What is the difference between equals and compareTo in Java?
- Difference between == and .Equals method in c#
- Java Program to Differentiate String == operator and equals() method
- What is the difference between operator and method on Python set?
- What is the difference between java method and native method?
- What is the difference between method hiding and method overriding in Java?
- What is the difference between method overloading and method hiding in Java?
- What is difference between a Java method and native method
- Difference between equals() vs equalsIgnoreCase() in Java
- Difference between concat() and + operator in Java
- What is the difference between a Java method and a native method?
- The equals and == operator for Enum data type in Java
- What is the difference between the dot (.) operator and -> in C++?
- What is the contract between equals() and hashCode() methods in Java?\n

Advertisements