Results 1 to 10 of 10
Thread: Question regarding generics
- 08-28-2009, 11:45 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 4
- Rep Power
- 0
Question regarding generics
Hi,
This question isn't exactly advanced; I feel it's more of an intermediate question. Anyway, from what I understand about java generics, the type information is stored only at compile-time i.e., only the variable contains the knowledge of type.
So in the following statement-
is just as valid asJava Code:List<Vector<Integer>> ls = new ArrayList<Vector<Integer>>();
But for the latter expression generates an warning, as if it lacks safety.Java Code:List<Vector<Integer>> ls = new ArrayList();
My question is, what difference do the two statements make, other than a few redundant letters?
The right-hand side actually provides little security -
All statements in the above generate neither warning nor an error in NetBeans. But every part of it is unsafe; the last line in the main function throws an exception.Java Code:public class Main { public static void main(String[] args) { List ls = new Vector<String>(); ls.add(42); ls = new Vector<Integer>(); ls.add("forty-two"); doStuff(ls); // throws exception } public static void doStuff(List<Float> ls) { Float f = (Float) ls.get(0); System.out.println(f); } }
So can anyone suggest a case where the generics-information in right-hand side actually helps? :confused:
EDIT: Nevermind, I figured it out...
List<String> ls = new Vector();
is same as
List obj = new Vector();
obj.add(3);
List<String> ls = obj;
This would make the warning necessary I guess..Last edited by Leaflord; 08-29-2009 at 07:08 PM.
- 08-29-2009, 12:18 AM #2
Backwards compatibility with legacy code that didn't have generics? Just guessing though.
My Hobby Project: LegacyClone
- 08-29-2009, 01:05 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,541
- Rep Power
- 11
- 08-29-2009, 07:42 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 08-29-2009, 12:40 PM #5
Member
- Join Date
- Aug 2009
- Posts
- 4
- Rep Power
- 0
I'm using JDK 1.6... Anyway the error wasn't thrown by NetBeans (as I've already said); but it still doesn't change the situation -
List<String> ls = new Vector<String>(); // throws error when ls.add(42)
List<String> ls = new Vector(); // warning + error for ls.add(42)
List ls = new Vector<String>(); // warnings in all cases
List ls = new Vector(); // warnings in all cases (despite no type specification)
That is to say, my example would throw errors even if I used a non-generic vector.
In other words, the generic type is solely deduced from variable and the object has no role to play in it.. Isn't the object's generic type redundant then?
- 08-29-2009, 01:12 PM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
So now you change and start to say that you do get warnings?
- 08-29-2009, 01:17 PM #7
Member
- Join Date
- Aug 2009
- Posts
- 4
- Rep Power
- 0
Care to read what I said, more carefully? In netbeans it doesn't show any warning. When I say "but", I mean upon manual compilation. The warnings are not generated upon generic-parameter-type mismatch; but if I use a non-generic Vector.
Instead of trying to point out to some assumed inconsistencies; why don't you try find a case where I get an exception for the second case (List<String> ls = new Vector(); ) but not the first case?
- 08-29-2009, 01:37 PM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Do you know where to check for warnings in Netbeans? Apparently not.
"but" does not mean upon manual compilation.
What exception case are you trying to look for? How about asking for clarification on the concepts that you clearly don't understand rather than setting pop exercises for people who are trying to help you.
When you do List<String> variableName, variableName becomes a List of Strings only and the compiler will make sure that you never add anything into it that is not a String.
Whether you initialise the list to new ArrayList<String> or new ArrayList() does not mean much except that you get a warning with the later. The list is still always going to be a List<String>.
- 08-29-2009, 01:52 PM #9
Member
- Join Date
- Aug 2009
- Posts
- 4
- Rep Power
- 0
*sigh* Instead of trolling why don't you answer my question?
Nevertheless; for warnings I'm simply relying on the wavy underline. Yes; I'm not a frequent netbeans user, I've started using it recently. If there's a better way of detecting errors then please do tell. If the "but" didn't imply that, then you should've assumed that rather than make useless conclusions.
What exception am I looking for? Can't you tell the exception from looking at the above code? Have you actually tried running the code?? The doStuff throws a ClassCastException because the first object in 'ls' is a String and not a Float.
How about you clearly say you don't know the answer? I'm not trying to set up pop exercises, I'm showing how I find it to be of no use and would like to know where that is actually useful.
Finally; you conclude that "the only difference is a warning" -- which is what I'm asking, WHY is it warning. And also; you're a bit wrong at the end. You mean to say that the variable is a List<String>, the object is a List.
- 08-29-2009, 02:24 PM #10
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
Generics & Inheritance Question
By Lee Rhodes in forum Advanced JavaReplies: 3Last Post: 07-03-2009, 05:04 AM -
Generics Question
By jdgallag in forum New To JavaReplies: 8Last Post: 10-28-2008, 06:15 PM -
Question about java generics
By Arrowx7 in forum New To JavaReplies: 1Last Post: 08-14-2008, 02:37 AM -
Help w/ generics
By Hollywood in forum New To JavaReplies: 2Last Post: 02-16-2008, 03:08 AM -
Generics
By sireesha in forum New To JavaReplies: 2Last Post: 01-10-2008, 11:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks