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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:27:15+00:00 2026-06-17T17:27:15+00:00

As you all very well know sometimes Java uses object pools for wrappers and

  • 0

As you all very well know sometimes Java uses object pools for wrappers and String types, sometimes it doesn’t.

For example:

Integer i1 = 1;
Integer i2 = 1;
Integer i3 = new Integer(1);
String s1 = "String";
String s2 = "String";
String s3 = new String ("String");
System.out.println("(i1 == i2) " + (i1 == i2));
System.out.println("(i2 == i3) " + (i2 == i3));
System.out.println("(s1 == s2) " + (s1 == s2));
System.out.println("(s2 == s3) " + (s2 == s3));

Execution result:

(i1 == i2) true
(i2 == i3) false
(s1 == s2) true
(s2 == s3) false

As you see boxing of primitives takes objects from the pool, creating of a Strings via string literal takes objects from the pool, too. Such objects are actually the same object (operator == returns true on them).

Other mechanisms of creating wrappers and Strings don’t take objects from the pool. Objects created these ways are actually different objects (operator == returns false on them).

What confuses me is the fact that the pool is used partially.

If its a memory issue, why not use the pool all of the times?
If its not a memory issue – why use it at all?

The question is – what are the reasons for implementing such behavior (= partial usage of the pool)?

The question is rather theoretical, but it has practical application – it can help understand how correctly use custom objects pools and of course understanding how Java works is always good.

  • 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-17T17:27:16+00:00Added an answer on June 17, 2026 at 5:27 pm

    Its a speed concern, allocating a new Integer every single time would be both time and memory expensive. But by the same token allocating at start up too much at start up uses tons of memory and time.

    Sadly it leads to some counter-intuitive behavior as you’ve found.

    The result is this weird compromise that we have. The reasons for this behavior are discussed in the Java standard. (5.7)

    If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.
    Ideally, boxing a given primitive value p, would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rules above are a pragmatic compromise. The final clause above requires that certain common values always be boxed into indistinguishable objects. The implementation may cache these, lazily or eagerly.

    For other values, this formulation disallows any assumptions about the identity of the boxed values on the programmer’s part. This would allow (but not require) sharing of some or all of these references.

    This ensures that in most common cases, the behavior will be the desired one, without imposing an undue performance penalty, especially on small devices. Less memory-limited implementations might, for example, cache all characters and shorts, as well as integers and longs in the range of -32K – +32K.

    tl;dr

    It’s impossible to make it work perfectly, and it’s too weird to not have it work at all. So we have it work for “most” of the time.

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

Sidebar

Related Questions

As we all know very well.. whenever we create application inside silverlight it is
I don't know Regex very well, and I'm trying to get all of the
when calling PQconnectdb in the main program all run very well, but if i
I can't search for a particular string, since they're all very similar, but I'd
I know ASP.NET C# very well. I make a lot of stuff like surveys
I am using PHP FOX all the process was documented very well. But need
The following code works very well when all involved classes are in the same
I'm pretty familiar with sed but I don't know awk very well, and I'm
Folks, I know I didn't phrase that title very well, but here's the scenario.
I dont know how to handle with DataSet very well (used to work with

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.