Blog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2025 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Java
Javaeasy

In Java, what’s the difference between `==` and `.equals()`?

Tags
#equals#reference#string
Back to categoryPractice quiz

Answer

`==` compares references for objects (same instance), while `.equals()` compares logical equality as defined by the class (e.g., `String` compares content). For primitives, `==` compares values.

String a = new String("hi");
String b = new String("hi");

System.out.println(a == b);       // false
System.out.println(a.equals(b));  // true

Related questions

Java
StringBuilder vs StringBuffer: what’s the difference?
#java#string#performance
Java
Why must `equals()` and `hashCode()` follow a contract?
#equals#hashcode#hashmap
Java
String vs StringBuilder vs StringBuffer — when to use which?
#string#stringbuilder#performance
Java
Why is String immutable in Java?
#string#immutability#memory
Algorithms
KMP string search: how does the LPS/prefix table avoid re-checking characters?
#kmp#string#pattern-matching