Results 1 to 5 of 5
Like Tree2Likes
  • 2 Post By JosAH

Thread: Generics question

  1. #1
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Generics question

    I'm following the java tutorial on generics and am wondering why this works:

    Java Code:
    import java.io.Serializable;
    import java.util.ArrayList;
    public class C5 {
    
        public static void main(String...args) {             
            Serializable s = pick("d", new ArrayList<String>());
        }
        
        static <T> T pick(T a1, T a2) { return a2; }    
        public <T> void foo(T t) {}
    
    }
    I would have thought that when you define a method like this

    Java Code:
     static <T> T pick(T a1, T a2)
    the compiler expects both arguments to be the same type, and the return type to be the same. Is that not the case?
    Here pick() is a method that returns a T and takes 2 arguments parameterized as the same type as each other (or so I thought) and the same type as the return type (or so I thought) but the arguments to pick are different types - String (a1) and ArrayList (a2).
    Why doesn't the compiler complain?
    Thanks.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default Re: Generics question

    That's type inference (a mixed blessing imho); because you didn't explicitly say anywhere what type T should be, the compiler tries to figure it out; it 'cleverly' found out that T == Serializable.

    kind regards,

    Jos
    gimbal2 and DarrylBurke like this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Generics question

    Whoa, I didn't realize it went that far. I don't like it when compilers become too clever, I would rather that it yells at me to be more specific so it doesn't have to derive anything.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default Re: Generics question

    Quote Originally Posted by gimbal2 View Post
    Whoa, I didn't realize it went that far. I don't like it when compilers become too clever, I would rather that it yells at me to be more specific so it doesn't have to derive anything.
    Yep, the compiler walks the entire class hierarchy (a tree), trying to find a common ancestor and it walks the entire interface DAG (Directed Acyclic graph) where it finds the answer: Serializabe; I don't remember what the compiler checks first: the tree or the DAG ... I don't think I like that inference game.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Re: Generics question

    Ah, I see. That's very interesting.

    Thanks very much.

Similar Threads

  1. Generics Question
    By La VloZ in forum New To Java
    Replies: 4
    Last Post: 07-04-2013, 10:20 AM
  2. Question on Generics
    By bimtownboy in forum New To Java
    Replies: 2
    Last Post: 05-01-2013, 02:24 PM
  3. Generics Question
    By erversteeg in forum New To Java
    Replies: 3
    Last Post: 02-11-2011, 05:13 AM
  4. Question regarding generics
    By Leaflord in forum Advanced Java
    Replies: 9
    Last Post: 08-29-2009, 02:24 PM
  5. Generics Question
    By jdgallag in forum New To Java
    Replies: 8
    Last Post: 10-28-2008, 06:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •