Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes What the use and benefit of writing also tell me the difference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "What the use and benefit of writing also tell me the difference" Watch "What the use and benefit of writing also tell me the difference" New topic
Author

What the use and benefit of writing also tell me the difference

satya pksh
Greenhorn

Joined: Jan 17, 2010
Posts: 8
What is the use and benefit of writing instead of

and also please tell me the difference of both.

While executing List<String> test = new ArrayList<String>(); it raise error that list cannot resolve to a type. I am confused because most of book write the ArrayList declaration in this way List<String> test = new ArrayList<String>();

reply
Thanks in advance

satya
Arpit Kumar Jain
Ranch Hand

Joined: Jun 05, 2010
Posts: 41
List<String> test = new ArrayList<String>();


As per the OOPS methodology, code should be designed with contract/interface and not implementation. Using this approach, implementation can be changed and user of this API do not have to change their code. It makes program more maintainable.

While executing List<String> test = new ArrayList<String>(); it raise error that list cannot resolve to a type.


Are you using any jdk version below 1.5 to compile this code, as Java SE will support Generics jdk 1.5 onwards.?

Thanks


SCJP, SCWCD, SCEA
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

This is the principle called: Program to an interface, not an implementation. This is a frequently asked question on the forums here.

The most important reason to declare the type of your variable as a List and not an ArrayList is to decouple the interface and the implementation in your program. The program only needs to know that the list is a List, it doesn't need to know what particular implementation of interface List is being used. Hiding the implementation means that you can easily change it later.

For example, you might find out later that for that particular list, a LinkedList would be more efficient than an ArrayList (these two implementations have different performance characteristics - for example, inserting elements in the middle is more efficient on a LinkedList than on an ArrayList). If your variable is declared as being a List, you would only need to change one line:

For the rest of the program, stuff is still a List.

If you would have made stuff an ArrayList, then it would be much harder to change it to a LinkedList, because there might be places in your program where ArrayList-specific methods are called on stuff.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Noting, of course, that using a concrete ArrayList *might* be correct in some cases.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What the use and benefit of writing also tell me the difference
 
Similar Threads
Polymorphic Declaration
Generics..
Generics help...
Java Collections 2
Generic Object Types vs Generic References