File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList" Watch "ArrayList" New topic


Rajat Sarkar

Joined: Sep 07, 2008
Posts: 18
ArrayList list =new ArrayList(); & ArrayList<Object> list = new ArrayList<Object>();
is both statement works same?is there any difference between them?
Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

You can add objects to both the lists. Only place where it breaks is when you assign any other type safe ArrayList to ArrayList<Object>. Something like this

SCJP 5.0 and now fighting with ExtJS
Marky Vasconcellos
Ranch Hand

Joined: Jan 28, 2009
Posts: 36
The difference was.. the compiler will claim you about he doens't know the type of objects will be in the first List.
anyway.. he assigns Object as Type but in the second case at least he know you provided this information.^^

Each of their nuggets of wisdom contracted to a sound bite: Joshua Bloch: Write Lots of Code; Chet Haase: Don't Put Your Entire Application in One Method; Masood Mortazavi: Start Simple and Keep Learning; Cay Horstmann: First, Don't Panic
I agree. Here's the link:
subject: ArrayList
It's not a secret anymore!