Results 1 to 10 of 10
  1. #1
    Leaflord is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default 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-

    Java Code:
    List<Vector<Integer>> ls = new ArrayList<Vector<Integer>>();
    is just as valid as
    Java Code:
    List<Vector<Integer>> ls = new ArrayList();
    But for the latter expression generates an warning, as if it lacks safety.

    My question is, what difference do the two statements make, other than a few redundant letters?

    The right-hand side actually provides little security -
    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);
    	}
    }
    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.

    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 08:08 PM.

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Backwards compatibility with legacy code that didn't have generics? Just guessing though.
    My Hobby Project: LegacyClone

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Quote Originally Posted by Leaflord View Post
    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.
    In fact the three lines
    Java Code:
    ls.add(42);
    ls.add("forty-two");
    doStuff(ls); // throws exception
    are all unsafe will give rise to a type safety warning at compile time unless those warnings have been suppressed somehow.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by Leaflord View Post
    ...
    All statements in the above generate neither warning nor an error in NetBeans. ...
    Either you have suppressed the warning or you are in fact using a compiler < 1.5

  5. #5
    Leaflord is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    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?

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    So now you change and start to say that you do get warnings?

  7. #7
    Leaflord is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    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?

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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>.

  9. #9
    Leaflord is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    *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.

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

Similar Threads

  1. Generics & Inheritance Question
    By Lee Rhodes in forum Advanced Java
    Replies: 3
    Last Post: 07-03-2009, 06:04 AM
  2. Generics Question
    By jdgallag in forum New To Java
    Replies: 8
    Last Post: 10-28-2008, 07:15 PM
  3. Question about java generics
    By Arrowx7 in forum New To Java
    Replies: 1
    Last Post: 08-14-2008, 03:37 AM
  4. Help w/ generics
    By Hollywood in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 04:08 AM
  5. Generics
    By sireesha in forum New To Java
    Replies: 2
    Last Post: 01-11-2008, 12:08 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
  •