File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Generics

 
Gireesh Giri
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Have a small doubt in usage of Generics.

ArrayList<Integer> al= new ArrayList();
ArrayList<Integer> al= new ArrayList<Integer>();

Can any one please tell me what is the difference between above two lines of code in detail.

My Observation:
1. In first line of code i can see the warning message (Type safety: The expression of type ArrayList needs unchecked conversion to conform to ArrayList<Integer>) in eclipse.


 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first line you're using the raw type ArrayList, without generics - that's why you get a warning. If you're using Java 7, there's a third syntax you can use:

ArrayList<Integer> al = new ArrayList<>();

Note the <>. That's the so-called "diamond operator". It exists only to save you some typing - you don't need to type <Integer> twice, the compiler will figure out by itself that you mean <Integer>.
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since of Java 7 , you can re-write your lines like this



Basically the use of generics provide you with the benefit of Stronger type checking at compile time and eliminates the use of casting . Letme give me you an example




Of-course you can still compile and run your code without the use of these generics.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47313
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saif Asif wrote: . . .



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.
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes you are right. I must have mistyped it. Gotta be more carefull next time ..

 
Gireesh Giri
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>();

Thanks and Regards,
Gireesh
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47313
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There should be something useful in the Java Tutorials.
 
Gireesh Giri
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Saif Asif. :)
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Anyway, let us try it with "you":



 
Campbell Ritchie
Sheriff
Pie
Posts: 47313
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I know how to get it to print u
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic