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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:59:21+00:00 2026-06-16T03:59:21+00:00

Integer.valueOf(int i) method contains assesrt to check that IntegerCache more or equals 127. public

  • 0

Integer.valueOf(int i) method contains assesrt to check that IntegerCache more or equals 127.

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);
}

The cache implementation looks like this

static {
        // high value may be configured by property
        int h = 127;
        String integerCacheHighPropValue =
            sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
        if (integerCacheHighPropValue != null) {
            int i = parseInt(integerCacheHighPropValue);
            i = Math.max(i, 127);
            // Maximum array size is Integer.MAX_VALUE
            h = Math.min(i, Integer.MAX_VALUE - (-low));
        }
        high = h;

        cache = new Integer[(high - low) + 1];
        int j = low;
        for(int k = 0; k < cache.length; k++)
            cache[k] = new Integer(j++);
    }

So you could increase cache size by passing property value. But implementation will not allows you to set cache size lower than 127 (or it does?). So why they place assertion in Integer.valueOf(). Do developers not trust own implementation?
I understand that if cache lower than 127 Integer.valueOf(int i) would return wrong value, but such just could not happen…

Is it necessary to have assert there?

  • 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-16T03:59:23+00:00Added an answer on June 16, 2026 at 3:59 am

    Do developers not trust own implementation?

    This is precisely the purpose of assertions: to catch programming bugs. One wouldn’t catch any bugs by assuming there are none.

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

Sidebar

Related Questions

Consider the following public method that adds an integer variable to a vector of
Why does the Integer.toString() method is implemented using String.valueOf(int i) instead of using directly
Integer i = 127; Integer j = 127; System.out.println(i == j); System.out.println(i.equals(j)); Integer i1
I am wondering why the method String.valueOf(int i) exists ? I am using this
Integer.toString(int); and String.valueOf(int); Which one among the above two methods is the efficient way
I've created my own MutableInt class, which contains a primitive int and some methods
requirement Assume the availability of an existing class, ICalculator, that models an integer arithmetic
Requirements Assume the availability of an existing class, ICalculator, that models an integer arithmetic
I am trying to convert an int into a String so that I can
I'm trying to create a lazy loading method using Java Lists that takes in

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.