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

  • Home
  • SEARCH
  • 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 8863259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:55:56+00:00 2026-06-14T15:55:56+00:00

Possible Duplicate: String object creation using new and its comparison with intern method I

  • 0

Possible Duplicate:
String object creation using new and its comparison with intern method

I was playing around with Strings to understand them more and I noticed something that I can’t explain :

String str1 = "whatever";
String str2 = str1;
String str3 = "whatever";
System.out.println(str1==str2); //prints true...that's normal, they point to the same object
System.out.println(str1==str3); //gives true..how's that possible ?

How is the last line giving true ? this means that both str1 and str3 have the same address in memory.

Is this a compiler optimization that was smart enough to detect that both string literals are the same (“whatever”) and thus assigned str1 and str3 to the same object ? Or am I missing something in the underlying mechanics of strings ?

  • 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-14T15:55:57+00:00Added an answer on June 14, 2026 at 3:55 pm

    http://www.xyzws.com/Javafaq/what-is-string-literal-pool/3

    As the post says:

    String allocation, like all object allocation, proves costly in both time and memory. The JVM performs some trickery while instantiating string literals to increase performance and decrease memory overhead. To cut down the number of String objects created in the JVM, the String class keeps a pool of strings. Each time your code create a string literal, the JVM checks the string literal pool first. If the string already exists in the pool, a reference to the pooled instance returns. If the string does not exist in the pool, a new String object instantiates, then is placed in the pool.

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

Sidebar

Related Questions

Possible Duplicate: Java Strings and StringPool I created two Strings String s1=MyString; String s2=new
Possible Duplicate: Why might a System.String object not cache its hash code? I always
Possible Duplicate: Java Strings: “String s = new String(”silly“);” What is the purpose of
Possible Duplicate: why does my compare method throw exception — Comparison method violates its
Possible Duplicate: difference between string object and string literal Let's say I have two
Possible Duplicate: Python: Get object by id Typically when you see the string representation
Possible Duplicate: Why is List<Number> not a sub-type of List<Object>? Isn't String a subtype
Possible Duplicate: String won't url encode in iOS I am using ASIHttpRequest to send
Possible Duplicate: String comparison in Objective-C I realize that the question is not very
Possible Duplicate: Extension Method Performance Will I face performance issues when I´m using extension

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.