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 6194107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:14:21+00:00 2026-05-24T03:14:21+00:00

I know this has been covered but I’ve seen inconsistent arguments here on SO.

  • 0

I know this has been covered but I’ve seen inconsistent arguments here on SO.

So if I have:

String a = "apple2e";
String b = "apple2e";

System.out.println("a==b? " + a == b);

I get FALSE.

As I understand it, it’s because a and b are two different references to the same object (apple2e).

So I would have something like:

a (reference_id 123) ------
                           ---------  "apple2e"
b (reference_id 456) ------

Now, if I just want to compare the contents of the two strings, I would use a.equals(b)

Does that mean that the JVM is simply returning if the two references are pointing to the same object? So it’s not really doing a character-by-character comparison?

Thanks

EDIT

Hold the phones. Thanks delnan for pointing out the + precedence!!!

When I change it to:

System.out.println(a == b);

I indeed get true.

This makes more sense.

EDIT 2

I can’t believe I didn’t catch that. lol

I was doing:

"a==b? " + a == b

Which translates to

"a==b? apple2e" == "apple2e"

No wonder it was false!!

  • 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-24T03:14:22+00:00Added an answer on May 24, 2026 at 3:14 am

    As I understand it, it’s because a and b are two different references to the same object (apple2e).

    Because of string interning, and only because of string interning a and b are different references to the same String object.


    Unfortunately, your code does not do what you think it does. Try this:

    String a = "apple2e";
    String b = "apple2e";
    
    System.out.println("a==b? " + a == b);    // "false"
    System.out.println("a==b? " + (a == b));  // "a==b? true"
    

    Java automatically interns all string literals. That is why the second sysout prints what it does. The first sysout prints only "false" because string concatenation (+) has higher precedence than ==, so it’s equivalent to this:

    System.out.println("a==b? apple2e" == "apple2e");
    

    I don’t think that’s what you meant to test!

    This, on the other hand, will give you two separate String instances:

    String a = new String("apple2e");
    String b = new String("apple2e");
    
    System.out.println("a==b? " + (a == b));  // "a==b? false"
    

    Which would schematically look like

    a (reference_id 123) ---------------  "apple2e"
    
    b (reference_id 456) ---------------  "apple2e"
    

    and can be reduced to the original situation using String#intern():

    String a = new String("apple2e").intern();
    String b = new String("apple2e").intern();
    
    System.out.println("a==b? " + (a == b));  // "a==b? true"
    

    e.g.

    a (reference_id 123) ------+
                               +---------  "apple2e"
    b (reference_id 456) ------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this subject has been covered before on here but I need a
I apologize because I know this has been covered over and over again, but
I know this has been covered elsewhere, but I'm new to the Android platform
I know this topic has been covered ad nauseam here, and other places on
I know this is a subject that has been well covered here and throughout
I know this has been covered so much but I'm a little out of
Ok guys, So I know this has been covered before, but what my question
i know this has been asked here . But my question is slightly different.
i know this has been answered lots on SO but i still have a
I know this has been asked in various ways, but I have not been

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.