aspose file tools*
The moose likes Beginning Java and the fly likes Doubt in Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in Generics " Watch "Doubt in Generics " New topic
Author

Doubt in Generics

Gireesh Giri
Greenhorn

Joined: Apr 23, 2010
Posts: 23
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

Joined: Aug 16, 2005
Posts: 14435
    
  23

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


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

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.


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40064
    
  28
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

Joined: Aug 11, 2011
Posts: 440

Oh yes you are right. I must have mistyped it. Gotta be more carefull next time ..

Gireesh Giri
Greenhorn

Joined: Apr 23, 2010
Posts: 23
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

Joined: Aug 11, 2011
Posts: 440

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

Joined: Oct 13, 2005
Posts: 40064
    
  28
There should be something useful in the Java Tutorials.
Gireesh Giri
Greenhorn

Joined: Apr 23, 2010
Posts: 23
Thanks Saif Asif. :)
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 908
    
    5
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

Joined: Oct 13, 2005
Posts: 40064
    
  28
But I know how to get it to print u
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Generics