Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1077165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:32:08+00:00 2026-05-16T21:32:08+00:00

Possible Duplicate: What makes reference comparison (==) work for some strings in Java? I

  • 0

Possible Duplicate:
What makes reference comparison (==) work for some strings in Java?

I know this has been asked before, but in spite of recommendations to use .equals() instead of the == comparison operator, I found that == works all the time:

String s1 = "Hello";
String s2 = "Hello";
System.out.println(s1 == s2); // true

Can anyone give me an example of the == operator failing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:32:08+00:00Added an answer on May 16, 2026 at 9:32 pm

    This is because you’re lucky. The == operator in Java checks for reference equality: it returns true if the pointers are the same. It does not check for contents equality. Identical strings found at compile-time are collapsed into a single String instance, so it works with String literals, but not with strings generated at runtime.

    For instance, "Foo" == "Foo" might work, but "Foo" == new String("Foo") won’t, because new String("Foo") creates a new String instance, and breaks any possible pointer equality.

    More importantly, most Strings you deal with in a real-world program are runtime-generated. User input in text boxes is runtime-generated. Messages received through a socket are runtime-generated. Stuff read from a file is runtime-generated. So it’s very important that you use the equals method, and not the == operator, if you want to check for contents equality.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Should I learn C before learning C++? As a professional (Java) programmer
Possible Duplicate: Is Java pass-by-reference? Java pass by reference For the following Java program,
Possible Duplicate: Reference - What does this symbol mean in PHP? I'm making a
Possible Duplicate: Reference - What does this symbol mean in PHP? Hi, I was
Possible Duplicate: Why doesn't the weakref work on this bound method? I'm using weakrefs
Possible Duplicate: Why doesn't the weakref work on this bound method? A bit of
Possible Duplicate: what happens to an object in Java if you do not reference
Possible Duplicate: Java - when to use 'this' keyword Is it generally good convention
Possible Duplicate: Make error: missing separator I am so stressed out by this silly
Possible Duplicate: Platform independent paths in Java I make a program but it save

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.