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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:20:36+00:00 2026-06-16T03:20:36+00:00

I got this one from a google I/O puzzler talk given by Joshua Bloch.

  • 0

I got this one from a google I/O puzzler talk given by Joshua Bloch. Here’s the code

 public class Glommer<T> {
      String glom(Collection<?> obj){
         String result = "";
         for(Object o : obj){
              result += o;
         }
         return result;
      }

      int glom(List<Integer> ints){
           int result = 0;
           for(int i : ints){
                result += i;
           }
           return result;
       }

      public static void main(String args[]){
           List<String> strings = Arrays.asList("1", "2", "3");
           System.out.println(new Glommer().glom(strings));
      }

this main method throws an exception because new Glommer is a raw type and hence all the generics in Glommer is erased, so it ends up calling int glom(List<Integer> ints) rather than String glom(Collection<?> obj).

My question is, even if I called glom() as new Glommer<Integer>().glom(strings) shouldn’t it call the int glom(List<Integer> ints) method since due to type erasure, this method is effectively int glom(List ints) and strings is of type List not Collection?

  • 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-16T03:20:37+00:00Added an answer on June 16, 2026 at 3:20 am

    The called method is defined at compilation time, not at runtime.

    If you add a parameter to your constructor call, the compiler will have enough information to know that it has to call the first method. Otherwise, it’s just as if generics didn’t exist. In both case, the called method will always stay the same at runtime.

    EDIT Some people seem to doubt, so here’s another example:

    public class Test {
    
        private static void test(Object object) {
            System.out.println("Object method");
        }
    
        private static void test(Integer integer) {
            System.out.println("Integer method");
        }
    
        public static void main(String[] args) {
            Object object = Integer.valueOf(0);
            test(object);
        }
    
    }
    

    The result is:

    Object method
    

    You pass an Integer to your method, but all that the compiler knows at compile time is that it’s an object. The jvm doesn’t automagically change the method call even though the Object is actually an Integer.

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

Sidebar

Related Questions

Here in Google App Engines I got this code that would help fetch an
This is a CSS related question, I got one good answer from my previous
I've got this code that uses a str_replace function: $result = mysql_query(SELECT * FROM
I got Calculator source code from google's android.git.kernel.org and successfully build the project without
This should be an easy one for folks. Google's got nothing except content farms
Google wasn't very helpful on this one, so I was hoping someone here might
This one's got me stumped and it's driving me nuts. I have a SiteFinity
I got couple of crash reports like this one and I'm not sure what
I got an installer which installs an application and starts this one right after
I can't figure this one out. I've got an iPhone-only app with an image

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.