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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:39:20+00:00 2026-05-27T15:39:20+00:00

Is doing: String a = new String(); String b = a; and doing: String

  • 0

Is doing:

String a = new String();
String b = a;

and doing:

String a = new String();
String b = a.intern();

is the same ?

Actually, the reference are all the same if I test:

String a = new String("te");
String b = a.intern();
String c = a;
String d = "t" + "e";
System.out.print(a.equals(b));
System.out.print(b.equals(c));
System.out.print(a.equals(d));

Cause the String will ever be in the String pool ?

  • 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-27T15:39:21+00:00Added an answer on May 27, 2026 at 3:39 pm

    Your equals tests aren’t checking for references – they’re checking for string equality. You should be using == to check the references for identity. Effectively, you’re making the common rookie Java mistake in reverse – usually people use == when they should be using equals. In this particular case, all of those will print false, because there are two String objects involved (the string from the constant pool, and the new string created in the first line). If we call these #1 and #2 respectively, we end up with:

    a = #2 // Explicit call to string constructor
    b = #1 // intern will return reference to constant pool instance
    c = #2 // Direct assignment
    d = #1 // Equivalent string constant, so reference to constant pool instance
    

    However, you may find this interesting:

    String a = "te";
    String b = a.intern();
    String c = "t" + "e";
    System.out.println(a == b); // true
    System.out.println(a == c); // true
    

    "te" and "t" + "e" are equal constant string expressions, so end up as references to a single string, and calling intern on a string already in the literal pool won’t have any effect.

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

Sidebar

Related Questions

I am doing something like this: class Class(object): def __init__(self): self.var=#new instance name string#
Assume you are doing something like the following List<string> myitems = new List<string> {
Doing this in a console program: object x = new string(new char[0]); If one
this is what i am doing: String[] output = new String[index.length]; try{ for(int i=0;
I'm quite new to nhibernate, I was doing all right until I face this
What are the use cases for doing new String(already a string) ? What's the
For doing string concatenation, I've been doing basic strcpy , strncpy of char* buffers.
Python is doing string multiplication where I would expect it to do numeric multiplication,
Is there any better alternative for doing string formatting in VC6, with syntax checking
When doing a string comparison in C#, what is the difference between doing a

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.