File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
satya pksh
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Arpit Kumar Jain
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
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
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noting, of course, that using a concrete ArrayList *might* be correct in some cases.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic