Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-24-2009, 04:52 AM
Member
 
Join Date: Feb 2009
Location: Sacramento CA
Posts: 17
Rep Power: 0
andre1011 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-24-2009, 05:26 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,451
Rep Power: 8
Fubarable is on a distinguished road
Default
What's the problem? Why can't you just use return type of int? Am I missing something here?

For e.g.,
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 05:41 AM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-24-2009, 06:21 AM
Member
 
Join Date: Feb 2009
Location: Sacramento CA
Posts: 17
Rep Power: 0
andre1011 is on a distinguished road
Default
It did not work at first but now it works like magic,
Thank You!
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-24-2009, 06:25 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,451
Rep Power: 8
Fubarable is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-24-2009, 10:21 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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.

andre1011, compare your code with what Fubarable post in his first replay here in this thread. May be you have miss something.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 02-24-2009, 05:10 PM
Steve11235's Avatar
Senior Member
 
Join Date: Dec 2008
Posts: 972
Rep Power: 2
Steve11235 is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 02-25-2009, 07:25 AM
Darryl.Burke's Avatar
Senior Member
 
Join Date: Sep 2008
Location: Madgaon, Goa, India
Posts: 733
Rep Power: 2
Darryl.Burke is on a distinguished road
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:
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 Darryl.Burke; 02-25-2009 at 07:28 AM.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 02-25-2009, 03:17 PM
Steve11235's Avatar
Senior Member
 
Join Date: Dec 2008
Posts: 972
Rep Power: 2
Steve11235 is on a distinguished road
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...
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
The Generic Trend this.that Advanced Java 9 02-26-2009 02:46 AM
Trouble with static methods and boolean equals() methods with classes dreamingofgreen New To Java 5 07-22-2008 07:34 AM
A generic interface example Java Tip java.lang 0 04-17-2008 08:42 PM
Writing generic methods eva New To Java 2 12-31-2007 04:28 AM
Generic Hashtables ShoeNinja New To Java 0 12-04-2007 11:43 PM


All times are GMT +2. The time now is 07:15 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org