PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Java · Theory

String Comparison in Java

← All stacks

Theory

75/270

String Comparison in Java

== compares references (same object in memory). equals compares characters. new String("Hi") twice → == is false, equals is true.

Literals "Hi" == "Hi" can be true because of the String Pool. User input and new String(...) almost never match with ==. Always equals for content.

equalsIgnoreCase ignores capital letters. compareTo gives ordering, not just true/false.

Let's take this on the board with one tiny Main class — no extra files. String Comparison in Java — output: false then true. new String("Hi") twice = two objects (== false). equals checks the text Hi. Start from main, go line by line, and stop at each print. That output is the proof for String Comparison.

Diagram
new String("Hi")   new String("Hi")
       │                  │
       └─ == false
       └─ equals true
Exam tip

Show new String("Hi") twice: == false, equals true. That is the whole answer.

Example

public class Main {
  public static void main(String[] args) {
    String a = new String("Hi");
    String b = new String("Hi");
    System.out.println(a == b);      // false (refs)
    System.out.println(a.equals(b)); // true (content)
  }
}

String Comparison in Java — output: false then true. new String("Hi") twice = two objects (== false). equals checks the text Hi.

Short notes

  • Def== = same object. equals = same characters.
  • RuleUser input → equals, not ==.
  • RememberPool can make two literals ==. new String breaks that.
  • TrapUsing == on strings from the keyboard / database.

Questions

1

== vs equals on String?

2

Two new String("Hi")?

Previous← Why String is Immutable in JavaNextString Concatenation in Java →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.