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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:09:52+00:00 2026-06-01T06:09:52+00:00

How is it possible that following code even compiles? As far as I can

  • 0

How is it possible that following code even compiles? As far as I can see the count function is called with two different types, yet compiler doesn’t complain and happily compiles this code.

public class Test {
        public static <T> int count(T[] x,T y){
                int count = 0;
                for(int i=0; i < x.length; i++){
                        if(x[i] == y) count ++;
                }
                return count;  
        }
        public static void main(String[] args) {
                Integer [] data = {1,2,3,1,4};
                String value = "1";
                int r =count(data,value);
                System.out.println( r + " - " + value);
        }
}
  • 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-01T06:09:54+00:00Added an answer on June 1, 2026 at 6:09 am

    In this case the T is useless. You can change the signature to public static int count(Object[] x, Object y) without any effect on what arguments the compiler will let it accept. (You can see that the signature for Arrays.fill() uses that as the signature.)

    If we consider the simpler case, where you just have arguments of type T, you can see that, since any instance of T is also an instance of its superclasses, T can always to be inferred to be its upper bound, and it will still accept the same argument types as before. Thus we can get rid of T and use its upper bound (in this case Object) instead.

    Arrays in Java work the same way: arrays are covariant, which means that if S is a subclass of T, S[] is a subclass of T[]. So the same argument as above applies — if you just have arguments of type T and T[], T can be replaced by its upper bound.

    (Note that this does not apply to generic types, which are not covariant or contravariant: List<S> is not a subtype of List<T>.)

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

Sidebar

Related Questions

Is it possible to transform following code to Linq that it will look like
Is it possible to write a C function that does the following? Allocate a
Possible Duplicate: Foreach can throw an InvalidCastException? Consider the following block of code public
I'm not sure if this is even possible. I've got the following code; <a
Given is the following code: function two() { return success; } function one() {
I was wondering if the following is possible. Create a class that accepts an
Following this question, it seems that it is possible to open a file from
If it possible to write byte code for a method that is supposed to
I have some performance-heavy code that performs bit manipulations. It can be reduced to
I have the following code in one of my js files $('#elementID').hoverIntent({ over: function()

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.