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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:15:01+00:00 2026-06-09T16:15:01+00:00

Here is a utility method I have: public static Class<?>[] getTypes(Object[] objects){ Class<?>[] types

  • 0

Here is a utility method I have:

public static Class<?>[] getTypes(Object[] objects){
    Class<?>[] types = new Class<?>[objects.length];

    for (int i = 0; i < objects.length; i++) {
        types[i] = objects[i].getClass();
    }

    return types;
}

And here is a test case that fails:

@Test
public void getTypesTest() {
    Object[] objects = {"String", new StringBuilder(), new Integer(5), 5};

    Class<?>[] classes = ReflectionUtils.getTypes(objects);
    assertTrue(classes[0].equals(String.class));
    assertTrue(classes[1].equals(StringBuilder.class));
    assertTrue(classes[2].equals(Integer.class));
    assertTrue(classes[3].equals(int.class)); // Fails here
}

I realize that when I pass 5 within Object[], this is Boxed to new Integer(5).

How can I get the behavior I am expecting?

Edit

What I am expecting: The assertion that fails in my test will pass. What should I do with the method under test to achieve that?:

  • 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-09T16:15:03+00:00Added an answer on June 9, 2026 at 4:15 pm

    You cannot treat a primitive as an object as this is what defines a primitive as different to an object. By having an array of objects, you are ensuring everything in it is an object, not a primitive.


    An int is not an object so you can’t put it in an Object[] so the compiler will generate code to auto-box you value of 5. i.e. it calls Integer.valueOf(5) which is more efficient than new Integer(5) but is still an object.

    You can’t call .equals() on a primitive so the fact this compiles tells you its not.

    BTW you can use int.class to get the class of int.

    How can I get the behavior I am expecting?

    Can you say exactly what you are expecting in English?


    For those interested, the code for Integer.valueOf(int) in Java 6 and 7

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a static method in a utility class that will download a file
I have a static utility method named toSet() which receives objects and returns a
I have a following static method in one of my utility class + (UIImage
I recently refactored some code and now have a static utility class with a
Let's say I have a utility class DateUtil (see below). To use this method
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }
I wrote a little utility class that saves BitmapSource objects to image files. The
Assume I have a static generic class. Its generic type parameters are not available
I am currently trying to call a method from a utility class that will
I have a utility method that goes through various classes and recursively retrieves 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.