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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:31:50+00:00 2026-06-17T15:31:50+00:00

The question boils down to this code: // setup String str1 = some string;

  • 0

The question boils down to this code:

// setup
String str1 = "some string";
String str2 = new String(str1);
assert str1.equals(str2);
assert str1 != str2;
String str3 = str2.intern();

// question cases
boolean case1 = str1 == "some string";
boolean case2 = str1 == str3;

Does Java standard give any guarantees about values of case1 and case2?
Link to relevant part of Java spec would be nice, of course.

Yes, I looked at all the “Similar Questions” found by SO, and found no duplicates, as none I found answered the question this way. And no, this is not about the misguided idea of “optimizing” string comparisons by replacing equals with ==.

  • 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-06-17T15:31:51+00:00Added an answer on June 17, 2026 at 3:31 pm

    Here is your JLS quote, Section 3.10.5:

    Each string literal is a reference (§4.3) to an instance (§4.3.1,
    §12.5) of class String (§4.3.3). String objects have a constant value.
    String literals-or, more generally, strings that are the values of
    constant expressions (§15.28)-are “interned” so as to share unique
    instances, using the method String.intern.

    Thus, the test program consisting of the compilation unit (§7.3):

    package testPackage;
    class Test {
            public static void main(String[] args) {
                    String hello = "Hello", lo = "lo";
                    System.out.print((hello == "Hello") + " ");
                    System.out.print((Other.hello == hello) + " ");
                    System.out.print((other.Other.hello == hello) + " ");
                    System.out.print((hello == ("Hel"+"lo")) + " ");
                    System.out.print((hello == ("Hel"+lo)) + " ");
                    System.out.println(hello == ("Hel"+lo).intern());
            }
    }
    
    class Other { static String hello = "Hello"; }
    

    and the compilation unit:

    package other;
    
    public class Other { static String hello = "Hello"; }
    

    produces the output: true true true true false true

    This example illustrates six points:

    Literal strings within the same class (§8) in the same package (§7)
    represent references to the same String object (§4.3.1).

    Literal strings within different classes in the same package represent
    references to the same String object.

    Literal strings within different classes in different packages
    likewise represent references to the same String object.

    Strings computed by constant expressions (§15.28) are computed at
    compile time and then treated as if they were literals.

    Strings computed by concatenation at run time are newly created and
    therefore distinct. The result of explicitly interning a computed
    string is the same string as any pre-existing literal string with the
    same contents.

    Combined with the JavaDoc for intern, and you have enough information to deduce that both of your cases will return true.

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

Sidebar

Related Questions

I have some code that, for the purposes of this question, boils down to
Ok I have some code that boils down to a pattern like this class
EDIT: Just to clarify, my main question boils down to this: if you issue
I'm struggling with ambiguous type variables in typeclasses in Haskell: This code boils down
My question boils down to this: is it standard structure in Swing programming to
The answer to this question boils down to two things: 1) Will it be
Question context: let say that there is some really important row in config/locales/en.yml that
Question is already been asked in title. Here is a code: (function($){ var filter
Question in title pretty much sums it up. I have some resource object defined
Question: I receive the following error for the code below, does anyone know why?

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.