Hi,

This is a part of a basic rule/pattern in java which says "programme to 
interface not an implementation".
If you say HashSet<String> s = new HashSet<String>(); you are tightly coupling 
your code to a Hashset,if in future you find Treeset more apt to your needs 
then HashSet your code has to undergo lot of changes and may lead to confusion.
On the other hand if we had used Set<String> s = new HashSet<String>(); then 
anytime in future you can choose a suitable Set implementation.
Hope i am clear.
http://www.artima.com/lejava/articles/designprinciples.html

Best Regards
Karthik




________________________________
From: JKid314159 <[email protected]>
To: Free Java Programming Online Training Course by Sang Shin 
<[email protected]>
Sent: Tuesday, 4 August, 2009 10:15:38 PM
Subject: [java programming] Lab1016Doc


Dear Java Programmer:
 
public class FindDups {
    public static void main(String[] args) {
      Set<String> s = new HashSet<String>();
        for (String a : args)
            if (!s.add(a))
                System.out.println("Duplicate detected: " + a);

        System.out.println(s.size() + " distinct words: " + s);
 
Why is the newly created HashSet of String elements set as implicitly cast to a 
string of Set.  Why not write the following:
 
HashSet<String> s = new HashSet<String>();
 
?
?
?
Respects,

JKid314159
http://existentialists.blogspot.com/
 
 
  



      See the Web&#39;s breaking stories, chosen by people like you. Check out 
Yahoo! Buzz. http://in.buzz.yahoo.com/
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to