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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:58:24+00:00 2026-05-23T09:58:24+00:00

Why is java.lang.Integer.valueOf a flyweight pattern? I tried to find the reason but wasn’t

  • 0

Why is java.lang.Integer.valueOf a flyweight pattern?
I tried to find the reason but wasn’t able to.

  • 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-23T09:58:24+00:00Added an answer on May 23, 2026 at 9:58 am

    If we look at the source for valueOf, we can get a hint:
    Source of java.lang.Integer lines 638-643:

    public static Integer valueOf(int i) {
            assert IntegerCache.high >= 127;
            if (i >= IntegerCache.low && i <= IntegerCache.high)
                 return IntegerCache.cache[i + (-IntegerCache.low)];
             return new Integer(i);
    }
    

    It looks like the Integer class maintains a cache of Integer objects for common values. Rather than make a new one every time somebody asks for valueOf, it just returns a reference to one that already exists. Therefore, if you call Integer.valueOf(1) multiple times, you’ll end up getting the same object back every time (not just equivalent objects).

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

Sidebar

Related Questions

How can I get an equivalent of java.lang.Integer.MIN_VALUE on C++?
Exception in thread Thread-2 java.lang.NumberFormatException: For input string: 3 int test = Integer.parseInt(result[0]); This
I've found that java.lang.Integer implementation of compareTo method looks as follows: public int compareTo(Integer
I often use java.lang.Integer as primary key. Here you can see some piece of
Any way to cast java.lang.Double to java.lang.Integer ? It throws an exception java.lang.ClassCastException: java.lang.Double
How large, in bytes, is a boxed primitive like java.lang.Integer or java.lang.Character in Java?
Consider this code: val foo = if(true) new java.lang.Double(4) else new java.lang.Integer(4) The inferred
While writing a game for J2ME we ran into an issue using java.lang.Integer.parseInt() We
I'm trying to create some XML like this: <parameter name=srid type=java.lang.Integer>24729</parameter> notice that the
java.lang.NullPointerException at java.lang.Integer.compareTo(Unknown Source) at java.lang.Integer.compareTo(Unknown Source) I slightly edited my code, deleted no

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.