Results 1 to 3 of 3
  1. #1
    salmontres is offline Member
    Join Date
    Dec 2011
    Posts
    30
    Rep Power
    0

    Default Question about declaring variables

    Hi guys,

    I have a basic question about declaring variables. I've read that when declaring a set, for instance, one wants to begin with the most basic declaration followed by something more specific, like so:

    Set<String> my_set = new HashSet<String>();

    Why is this better than doing the following:

    HashSet<String> my_set = new HashSet<String>();

    It seems like a trifle, and it may be, but I still don't understand the advantages of the first case.

  2. #2
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Question about declaring variables

    Imagine a third party software vendor made a hash set that was far more efficient than the one you have. You could just swap it in like this:

    import org.betterSofware.utilities.*

    Set<String> my_set = new BetterHashSet<String>();

    But if you had this..

    HashSet<String> my_set = new HashSet<String>();

    you would have to trawl through your code base to see where people had used my_set as a HashSet, e.g

    HashSet<String> my_other_set = my_set;

    because swapping in a BetterHashSet would invalidate such code, wherever it appeared.

  3. #3
    salmontres is offline Member
    Join Date
    Dec 2011
    Posts
    30
    Rep Power
    0

    Default Re: Question about declaring variables

    Got it, thanks 2by4!

Similar Threads

  1. Replies: 3
    Last Post: 08-07-2011, 03:31 AM
  2. Why declaring Swing variables in longform
    By africanhacker in forum New To Java
    Replies: 4
    Last Post: 02-06-2011, 10:35 AM
  3. OOP Question re. private variables and extending classes
    By ImplicitCharm in forum New To Java
    Replies: 7
    Last Post: 07-28-2009, 04:46 PM
  4. Declaring variables
    By Flashmatix in forum New To Java
    Replies: 12
    Last Post: 01-20-2009, 04:43 PM
  5. Declaring global variables
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 01:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •