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

  • Home
  • SEARCH
  • 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 586747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:08:21+00:00 2026-05-13T15:08:21+00:00

As per my understanding the following generic function in java: public static <T> T

  • 0

As per my understanding the following generic function in java:

public static <T> T f(T x) {
   Integer[] arr = new Integer[4];
   T ret = (T) arr[2];
   return ret;
}

is compiled to the following form (as it is unbounded):

public static Object f(Object x) {
   Integer[] arr = new Integer[4];
   Object ret = (Object) arr[2];
   return ret;
}

However, when I run the following statement, the compiler is able to figure out the return value to be Integer type. How does the compiler figure it out?

Integer i = f(new Integer(4));

Shouldn’t the function be written as following for the above statement to work?

  public static <T extends Integer> T f(T x) {
       Integer[] arr = new Integer[4];
       T ret = (T) arr[2];
       return ret;
    }
  • 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-13T15:08:21+00:00Added an answer on May 13, 2026 at 3:08 pm

    Generics use type erasure. That basically means that generics are nothing more than implicit casts so when you do:

    List<Integer> ...
    

    it’s no different to a normal List and may contain Integers or anything really. You’re just telling Java to cast get() to an Integer (and other things). The type simply isn’t retained at runtime (mostly).

    Arrays are different. Arrays are what’s called covariant. That means their type is retained at runtime. So you can do:

    List<Integer> list1 = new ArrayList<Integer>();
    list2 = (List<String>)list1;
    list2.add("hello");
    

    which is perfectly legal and will compile and run. But:

    Integer[] arr1 = new Integer[10];
    String[] arr2 = (String[])arr1; // compiler error
    

    But it gets more subtle than that too.

    Integer[] arr1 = new Integer[10];
    Object[] arr2 = (Object[])arr1;
    arr2[5] = "hello"; // runtime error!
    

    As to your function. When you write:

    public static <T> T f(T x) {
      Integer[] arr = new Integer[4];
      T ret = (T) arr[2];
      return ret;
    }
    

    you’re telling the compiler to derive T, being the argument type and the return type, from the argument. So when you pass in an Integer the return type is Integer. When you call:

    Integer i = f(new Integer(4));
    

    the compiler is simply following your instructions. The function does take and return an Object in compiled form but it just does this:

    Integer i = (Integer)f(new Integer(4));
    

    implicitly.

    Just like the List example above there is nothing stopping you have f() return anything you like rather than what it’s supposed to return based on the parameterized type.

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

Sidebar

Ask A Question

Stats

  • Questions 344k
  • Answers 344k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer python /var/www/hash_info.py /var/www/Muha_Album.7912.torrent May 14, 2026 at 5:38 am
  • Editorial Team
    Editorial Team added an answer Try <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("Name") + "," + Eval("Surname")… May 14, 2026 at 5:38 am
  • Editorial Team
    Editorial Team added an answer A standard set of error codes and it's corresponding messages… May 14, 2026 at 5:38 am

Related Questions

I just got done working through the Django tutorials for the second time, and
I'm digitally signing a XML document and verifying it on 2 different machines (Sign
The following question is inspired by this blog entry at ajaxian.com and this comment
I have an NSDocument which has the following structure: @interface MyDocument : NSDocument {
I'm using VB 2008 and I'm trying to add a xmlns=mynamespace attribute to an

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.