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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:30:20+00:00 2026-06-07T11:30:20+00:00

Possible Duplicate: Java: Array of primitive data types does not autobox I’ve been doing

  • 0

Possible Duplicate:
Java: Array of primitive data types does not autobox

I’ve been doing some Java lately (mostly as a recap on what I did at university as my current job doesn’t involve any Java at all) and one thing that never got introduced to us properly is Generics.

I’ve been doing the Generics tutorial on the Oracle Java website and I’ve been stumped by the following example that I modified a bit:

public static <Integer extends Comparable<Integer>> int countGreaterThan(Integer [] anArray, Integer elem) {
    int count = 0;
    for (Integer e : anArray)
        if (e.compareTo(elem) > 0)
            ++count;
        return count;
}

And I use this method to compare an Integer array with the following Integer:

Integer [] numarr = {1, 2, 3, 4 , 5};
Integer num = new Integer(5);

int count = Util.countGreaterThan(arrnum, num);

This seems to work fine, however if I pass a primitive array and a primitive int, it doesn’t:

int [] arrnum = {1, 2, 3, 4 , 5};
int othernum = 3;

int count = Util.countGreaterThan(arrnum, othernum);

The compiler complains with:

method countGreaterThan in class Generics.Util 
cannot be applied to given types;
required: Integer[],Integer
found: int[],int
reason: no instance(s) of type variable(s) 
Integer exist so that argument type int conforms to formal parameter type Integer

The tutorials seemed to be pretty adamant that Java will always autobox/unbox objects and primitives as necessary and yet it won’t do it in this particular case, what am I missing?

Also what could be a good way to generalise this comparing method as much as possible? If I used T instead of Integer, then it would look for a T object, rather than whatever I pass to it.

I apologise if the post is confusing and if I seem pretty ignorant about the stuff I talk about above, it’s just that I’ve been working primarily as a perl programmer (and not as particularly experienced one either) and generalising there seems as less of an issue (due to the lack of type enforcement).

Thanks in advance!

  • 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-07T11:30:21+00:00Added an answer on June 7, 2026 at 11:30 am

    The code below seems to work as far as I can see:

    public static <Integer extends Comparable<Integer>> int countGreaterThan(Integer [] anArray, Integer elem) {
        int count = 0;
        for (Integer e : anArray) {
            if (e.compareTo(elem) > 0)
                ++count;
            return count;
    }
    

    although I get a warning "The type parameter Integer is hiding the type Integer". See @ErichSchreiner‘s answer for an explanation of why auto-boxing won’t work here.

    A good generalisation of your method would be:

    public static <T extends Comparable<? super T>> int countGreaterThan(T[] anArray, T elem) {
        int count = 0;
        for (T e : anArray) {
            if (e.compareTo(elem) > 0) {
                ++count;
            }
        }
        return count;
    }
    

    All you really care about is that anArray has the same type of elements as elem is, and that it is comparable to it’s own type.

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

Sidebar

Related Questions

Possible Duplicate: What’s the reason I can’t create generic array types in Java? HashSet<Integer>[]
Possible Duplicate: Byte array in objective-c Am converting some Java code to Objective-C and
Possible Duplicate: Sort arrays of primitive types in descending order Java : How to
Possible Duplicate: Java generics and array initialization How does one instantiate an array of
Possible Duplicate: Java - Regex problem I have list of URLs of types: http://www.example.com/pk/etc
Possible Duplicate: How to declare an array in Java? Suppose I have an object
Possible Duplicate: Why does (360 / 24) / 60 = 0 … in Java
Possible Duplicate: Java how to: Generic Array creation Error generic array creation I have
Possible Duplicate: Java - Distinct List of Objects i have a sorted array of
Possible Duplicate: JSON Array iteration in Android/Java I am fetching JSON string from server

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.