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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:19:53+00:00 2026-05-16T06:19:53+00:00

Usually the compiler generates code to perform boxing and unboxing. But what does the

  • 0

Usually the compiler generates code to perform boxing and unboxing. But what does the compiler, if the boxed values are not needed? Is the (Oracle standard) compiler smart enough to optimize it away?

Take a look at this method:

public static void requireInRange(int index, Object[] array) {
    if(index < 0 || index >= array.length)
        throw new IndexOutOfBoundsException();
}

The only relevant information is the array.length, so it would be useless to box each value of an array for example. Like in this code:

int[] anArray = {3, 4, 2};
requireInRange(3, anArray);

Will the compiler actually insert code for boxing each value of the array?

  • 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-16T06:19:53+00:00Added an answer on May 16, 2026 at 6:19 am

    There is no autoboxing in your code. In fact, given:

    public static void requireInRange(int index, Object[] array) {
       ...
    }
    
    int[] anArray = {3, 4, 2};
    requireInRange(3, anArray); // DOES NOT COMPILE!!!
    

    While an int can be autoboxed to an Integer, an int[] does NOT get autoboxed to Integer[] by Java. You can write library functions to do this, but the language will not facilitate this conversion.

    This is in fact the source of many confusion regarding e.g. Arrays.asList(anIntArray) being “broken”, because instead of returning a List<Integer>, what is returned is in fact a one-element List<int[]>.


    But what about performance???

    A quote from Java Language Guide/Autoboxing:

    It is not appropriate to use autoboxing and unboxing for scientific computing, or other performance-sensitive numerical code. An Integer is not a substitute for an int; autoboxing and unboxing blur the distinction between primitive types and reference types, but they do not eliminate it.

    In short, whenever autoboxing happens, performance definitely takes a little bit of a hit. Certain things help to alleviate this, e.g. the caching mechanism built into these types. This is why you get the following:

        System.out.println(
            ((Integer) 0) == ((Integer) 0)
        );
        // true
    
        System.out.println(
            ((Integer) 10000) == ((Integer) 10000)
        );
        // false (implementation-specific)
    

    What happened here is that when 0 is automatically boxed, no new Integer instance is actually created: values in certain range is cached for autoboxing purposes, to help performance. 10000 in most implementation probably falls out of this range, but some JVM implementations do allow you to specify the cache range if necessary.


    But I just want to get the length of the array!!!

    There are many ways to facilitate your requireInRange to work with any type of arrays. Unfortunately working with Java’s array of primitives often times mean lots of repetition. This mean providing overloads for int[], boolean[], byte[], Object[], etc separately.

    A more concise option is to use reflection, but this has its pluses and minuses. Generally speaking, reflection should not be the preferred solution for most scenarios.

    Having said that, java.lang.reflect.Array does have a int getLength(Object array) static method that can return the length of ANY array. It’s not typesafe (like most reflection mechanism are); passing a non-array compiles, but throws IllegalArgumentException at run-time.

    Related questions

    • Managing highly repetitive code and documentation in Java – (“inspired” by java.util.Arrays)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can usually understand the reason behind a compiler warning, but this one seems
I remember when I was developing in C++ or Java, the compiler usually complains
Usually I worked with PostgreSQL and never had a problem, but now I need
Usually, as my code base grows, the functions recieve more and more arguments and
From what I have read java (usually) seems to compile java to not very
I usually write C code in C89, now some features of C99 (like intxx_t
I'm working on a piece of software which generates assembler code at runtime. For
Usually I don't have problems writing assembly and testing and debugging. But then sometimes
Languages like Ruby and Python are usually referred-to as open source, but what makes
I'd like to modify my compiler's code generator to use visitor pattern since the

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.