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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:03:17+00:00 2026-05-23T15:03:17+00:00

The code below should not print Bye, since == operator is used to compare

  • 0

The code below should not print “Bye”, since == operator is used to compare references, but oddly enough, “Bye” is still printed. Why does this happen? I’m using Netbeans 6.9.1 as the IDE.

public class Test {
    public static void main(String [] args) {
        String test ="Hi";
        if(test=="Hi"){
            System.out.println("Bye");
        }
    }
}
  • 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-23T15:03:17+00:00Added an answer on May 23, 2026 at 3:03 pm

    This behavior is because of interning. The behavior is described in the docs for String#intern (including why it’s showing up in your code even though you never call String#intern):

    A pool of strings, initially empty, is maintained privately by the class String.

    When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

    It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

    All literal strings and string-valued constant expressions are interned. String literals are defined in §3.10.5 of the Java Language Specification.

    So for example:

    public class Test {
    
        private String s1 = "Hi";
    
        public static void main(String [] args) {
    
            new Test().test();
            System.exit(0);
        }
    
        public void test() {
            String s2 ="Hi";
            String s3;
    
            System.out.println("[statics]          s2 == s1? " + (s2 == s1));
            s3 = "H" + part2();
            System.out.println("[before interning] s3 == s1? " + (s3 == s1));
            s3 = s3.intern();
            System.out.println("[after interning]  s3 == s1? " + (s3 == s1));
            System.exit(0);
        }
    
        protected String part2() {
            return "i";
        }
    }
    

    Output:

    [statics]          s2 == s1? true
    [before interning] s3 == s1? false
    [after interning]  s3 == s1? true

    Walking through that:

    1. The literal assigned to s1 is automatically interned, so s1 ends up referring to a string in the pool.
    2. The literal assigned to s2 is also auto-interned, and so s2 ends up pointing to the same instance s1 points to. This is fine even though the two bits of code may be completely unknown to each other, because Java’s String instances are immutable. You can’t change them. You can use methods like toLowerCase to get back a new string with changes, but the original you called toLowerCase (etc.) on remains unchanged. So they can safely be shared amongst unrelated code.
    3. We create a new String instance via a runtime operation. Even though the new instance has the same sequence of characters as the interned one, it’s a separate instance. The runtime doesn’t intern dynamically-created strings automatically, because there’s a cost involved: The work of finding the string in the pool. (Whereas when compiling, the compiler can take that cost onto itself.) So now we have two instances, the one s1 and s2 point to, and the one s3 points to. So the code shows that s3 != s1.
    4. Then we explicitly intern s3. Perhaps it’s a large string we’re planning to hold onto for a long time, and we think it’s likely that it’s going to be duplicated in other places. So we accept the work of interning it in return for the potential memory savings. Since interning by definition means we may get back a new reference, we assign the result back to s3.
    5. And we can see that indeed, s3 now points to the same instance s1 and s2 point to.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below should print ten results but instead it prints ten test-ite. Why
The code below is from an old Perl script. print %{@{$noss}[$i]}->{$sector} \n\n; How should
When retrieving objects from an NSMutableArray in cocoa-touch is the below code ok? Should
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
Code below is not working as expected to detect if it is in design
The code below shows a sample that I've used recently to explain the different
The code below works. But if I comment out the line Dim objRequest As
The code below gives an error: Property 'Int32 Key' is not defined for type
The code below is used to calculate the miles between two cities. In this
The code below gives me this mysterious error, and i cannot fathom it. I

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.