Results 1 to 4 of 4
  1. #1
    spartan is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default Help in Genric methods

    Hi,

    I have a program like this

    public class Box {


    public <U,V> void typeInference(U u, V v1, V v2) {
    Set<V> s1 = new HashSet<V>();
    System.out.println(u.getClass().getName());
    System.out.println(v1.getClass().getName());
    System.out.println(v2.getClass().getName());
    s1.add(v1);
    s1.add(v2);
    System.out.println(s1);


    }

    public static void main(String[] args) {

    Box box = new Box();

    box.typeInference(new Integer(10), new Integer(30), new Strin("abc"));

    }
    }

    Output :

    java.lang.Integer
    java.lang.Integer
    java.lang.String
    [abc, 30]

    Could anyone explain this is correct or not. What type of set is "s1" ie Integer Set or String set

    Thanks,
    Guru

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    10

    Default

    ) It's cool. You just forget to set type for generic. In this code you just use wildcard and it's ok. but If you try do this. see
    Java Code:
     box.<String, String>typeInference(new Integer(10), new Integer(30), new String("abc"));
    You will have compile error.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    spartan is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    Thanks Petr. I was trying to unserstand Type Inference and I referred to the following link
    Type Inference (The Java™ Tutorials > Learning the Java Language > Generics)

    The tutorail says like this "the Java compiler can infer the type parameters of a generic method call".


    I was expecting

    "box.typeInference(new Integer(10), new Integer(30), new Strin("abc"));" to fail, but it succeded. Could you please explain where I went wrong.

    Thanks,
    Guru

  4. #4
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    10

    Default

    it's normal. when you pass some parameters in generic methods you don't declare a generic type and compiler thinks that this is just Object type. for example
    Java Code:
    Set s1 = new HashSet();
    s1.add(new Integer(10));
    s1.add(new String("abc"));
    it's ok too.
    Skype: petrarsentev
    http://TrackStudio.com

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Help with Methods Please :-)
    By jkhamler in forum New To Java
    Replies: 22
    Last Post: 11-10-2009, 08:54 PM
  3. Replies: 4
    Last Post: 04-03-2009, 06:36 AM
  4. Get and Set Methods
    By kian_hong2000 in forum New To Java
    Replies: 10
    Last Post: 08-27-2008, 02:06 PM
  5. methods
    By Zensai in forum New To Java
    Replies: 10
    Last Post: 12-03-2007, 05:31 AM

Posting Permissions

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