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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:45:16+00:00 2026-06-07T18:45:16+00:00

If I have an array of instances of primitive wrapper classes, how can I

  • 0

If I have an array of instances of primitive wrapper classes, how can I obtain an array of their respective primitives?

Object toPrimitiveArray(Object[] wrappedArray) {
    return ?;
}

Object primitiveArray = toPrimitiveArray(new Integer[] { 1, 2, 3 });

In the above example, I’d like toPrimitiveArray() to return an int[] containing the int values 1, 2, and 3.

  • 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-07T18:45:18+00:00Added an answer on June 7, 2026 at 6:45 pm

    The problem is that your method cannot know what type of Number is being contained by the array, because you have defined it only as an array of Object, with no further information. So it’s not possible to automatically produce a primitive array without losing compile-time type safety.

    You could use reflection to check the Object type:

    if(obj instanceof Integer) {
        //run through array and cast to int
    } else if (obj instanceof Long) {
        //run through array and cast to long
    } // and so on . . .
    

    However, this is ugly and does not allow the compiler to check for type safety in your code, so increases the chance of error.

    Could you switch to using a List of Number objects instead of an array? Then you could use type parameters (generics) to guarantee type safety.

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

Sidebar

Related Questions

I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]
I have an array filled with instances of a custom class which contains two
I have an array of instances of e.g. class Result, I want to join
so i have an array containing many instances. let's say movieclips. and i have
I have an array ( Items ) which holds lots of instances of a
I have a method which converts the Object ( which is an array of
In one of my Java 6 projects I have an array of LinkedHashMap instances
I have an array with close to 1000 instances of a class, each of
I have an app holding an array of instances of a class called SwimmingPool.
HI all. I have an array of BackgroundWorker objects running instances of a Worker

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.