• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

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

 
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
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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Noting, of course, that using a concrete ArrayList *might* be correct in some cases.
 
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic