This question has already been answered:
And yet, somebody can explain in detail what is different with Set<String> set1 = new HashSet<String>(); and HashSet<String> set1 = new HashSet<String>(); ?
This question has already been answered:
And yet, somebody can explain in detail what is different with Set<String> set1 = new HashSet<String>(); and HashSet<String> set1 = new HashSet<String>(); ?
java.util.Set is an interface, not a class.HashSet implements the Set interface.Set set1 = new HashSet(); creates an object of type HashSet and assigns a reference to the object of a variable of type Set .
HashSet set1 = new HashSet(); creates an object of type HashSet and assigns a reference to the object of a variable of type HashSet .
That is, these two examples of initialization differ in the way HashSet stored. For example, initialization Set set1 = new HashSet(); allows you to "redo" HashSet in TreeSet .
PS I forgot to add that in OOP it is considered good form to declare variables with an interface type, since code needs to be written in the most general form.
Source: https://ru.stackoverflow.com/questions/588186/
All Articles