Results 1 to 8 of 8

Thread: Generic methods

  1. #1
    andre1011 is offline Member
    Join Date
    Feb 2009
    Location
    Sacramento CA
    Posts
    17
    Rep Power
    0

    Default Generic methods

    How can I have a generic method return a primitive data type such as an 'int' ?
    I am just counting the number of elements in an array and returning that value as an integer

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What's the problem? Why can't you just use return type of int? Am I missing something here?

    For e.g.,
    Java Code:
    public class Fubar3
    {
      public static <U> int myMethod(U[] u)
      {
        return u.length;
      }
      
      public static void main(String[] args)
      {
        String[] strings = {"uno", "dos", "tres"};
        
        System.out.println(myMethod(strings));
      }
    }
    myMethod returns an int with ease.
    Last edited by Fubarable; 02-24-2009 at 04:41 AM.

  3. #3
    andre1011 is offline Member
    Join Date
    Feb 2009
    Location
    Sacramento CA
    Posts
    17
    Rep Power
    0

    Default

    It did not work at first but now it works like magic,
    Thank You!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hm, I don't know why it didn't work and now it does, nor do I know if I did anything other than wave my magic Java wand, but you're welcome.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    Hm, I don't know why it didn't work and now it does, nor do I know if I did anything other than wave my magic Java wand, but you're welcome.
    Yep, it wont be a magic at all. :p

    andre1011, compare your code with what Fubarable post in his first replay here in this thread. May be you have miss something.

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    So, Fubarable's code specifies that U is an unspecified Type; U[] allows any sort of array. This seems to have the same effect as simply declaring the parameter as Object[]. Is that true?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,247
    Rep Power
    19

    Default

    In this case, yes, the parameter could be declared as Object[]. But. If the class is generic, type-safety will be enforced at compile time.

    A minor change to Fu's code:
    Java Code:
    public class Fubar3<U>
    {
      public int myMethod(U[] u)
      {
        return u.length;
      }
      
      public static void main(String[] args)
      {
        String[] strings = {"uno", "dos", "tres"};
        
        new Fubar3<String> fubar3 = new Fubar3<String>();
        System.out.println(fubar3.myMethod(strings));
    
        Integer[] ints = {1, 2, 3};
        System.out.println(fubar3.myMethod(ints)); // compile time error
      }
    }
    Don't know whether I made things more obscure.

    There are two Generics tutorials on the Sun site, elementary and advanced. And the relevant section in the JLS is also worth reading.

    db
    Last edited by DarrylBurke; 02-25-2009 at 06:28 AM.

  8. #8
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Thanks, DB. What you did is what I'm used to. I was under the impression that generic types had to be defined at the class level, as you showed. Then, a specific implementation of that class can specify a Type, which will be enforced.

    I hadn't seen a generic defined, as opposed to referenced, in a method signature before. I'm not sure I see an value in that approach...

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. The Generic Trend
    By this.that in forum Advanced Java
    Replies: 9
    Last Post: 02-26-2009, 01:46 AM
  3. A generic interface example
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:42 PM
  4. Writing generic methods
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 03:28 AM
  5. Generic Hashtables
    By ShoeNinja in forum New To Java
    Replies: 0
    Last Post: 12-04-2007, 10:43 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
  •