Results 1 to 8 of 8
Thread: Generic methods
- 02-24-2009, 03:52 AM #1
Member
- Join Date
- Feb 2009
- Location
- Sacramento CA
- Posts
- 17
- Rep Power
- 0
-
What's the problem? Why can't you just use return type of int? Am I missing something here?
For e.g.,
myMethod returns an int with ease.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)); } }Last edited by Fubarable; 02-24-2009 at 04:41 AM.
- 02-24-2009, 05:21 AM #3
Member
- Join Date
- Feb 2009
- Location
- Sacramento CA
- Posts
- 17
- Rep Power
- 0
It did not work at first but now it works like magic,
Thank You!
-
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.
- 02-24-2009, 09:21 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 02-24-2009, 04:10 PM #6
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?
- 02-25-2009, 06:25 AM #7
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:Don't know whether I made things more obscure.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 } }
There are two Generics tutorials on the Sun site, elementary and advanced. And the relevant section in the JLS is also worth reading.
dbLast edited by DarrylBurke; 02-25-2009 at 06:28 AM.
- 02-25-2009, 02:17 PM #8
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
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
The Generic Trend
By this.that in forum Advanced JavaReplies: 9Last Post: 02-26-2009, 01:46 AM -
A generic interface example
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:42 PM -
Writing generic methods
By eva in forum New To JavaReplies: 2Last Post: 12-31-2007, 03:28 AM -
Generic Hashtables
By ShoeNinja in forum New To JavaReplies: 0Last Post: 12-04-2007, 10:43 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks