Of-course you can still compile and run your code without the use of these generics.
Not at all. The JVM will never see that code, because it fails to compile. I think your line 6 will fail to compile, too.
That sort of code is what we used to write in Java1.4. When generics was introduced (remember older languages had used generics for decades before that), those errors were moved from runtime errors to compiler errors. Remember runtime errors can be harmful and compiler errors are never harmful. Now generics is available, you should always use it.
Oh yes you are right. I must have mistyped it. Gotta be more carefull next time ..
Joined: Apr 23, 2010
Thanks All for Reply,
My doubt is, any functionality difference between these below two lines of code in java6 other than warning message at first syntax
ArrayList<Integer> al= new ArrayList(); // having generics on declaration not on object creation
ArrayList<Integer> al= new ArrayList<Integer>();
No I don't think there wont be any 'functionality' difference since the JVM will perform auto-casting on both. I recommend using the second method to declare and initialize a properly parameterized type.
It is a compile time story: they get compiled to the same sequence of instructions.
First I tried to demonstrate this with u.java, but the forum software rejected it:
> We're sorry, but your post appears to contain abbreviations that we don't like people to use at the Ranch.
> Because JavaRanch is an international forum, many of our members are not native English speakers.
> For that reason, it's important that we all try to write clear, standard English, and avoid abbreviations and SMS shortcuts.
> See here for more of an explanation. Thanks for understanding.
> If the abbreviation occurs within code, you can use code tags to post it successfully.
> The specific error message is: "u" is a silly English abbreviation; use "you" instead.