• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in Generics

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>.
 
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There should be something useful in the Java Tutorials.
 
Gireesh Giri
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Saif Asif. :)
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I know how to get it to print u
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic