- Feb 8, 2004
- 12,604
- 15
- 81
Ive just found out that this works fine:
Set aSet;
aSet = new HashSet();
aSet.add(1);
aSet.add(2);
aSet.add("boobs");
System.out.println(aSet);
output: [1, 2, boobs]
Makes sense since these collection classes only store objects right? Whats the point in declaring a type for them? Also can someone explain the following:
Set<Integer> aSet;
aSet = new HashSet();
aSet.add(1);
aSet.add(2);
aSet.add("boobs");
System.out.println(aSet);
That dosent let me put in a string and dosent compile.
Set aSet;
aSet = new HashSet<Integer>();
aSet.add(1);
aSet.add(2);
aSet.add("boobs");
System.out.println(aSet);
This does let me put in a string and compiles fine. What gives?
Is declaring types for readability reasons and to prevent stuff that's not supposed to be in the set from being put in?
Set aSet;
aSet = new HashSet();
aSet.add(1);
aSet.add(2);
aSet.add("boobs");
System.out.println(aSet);
output: [1, 2, boobs]
Makes sense since these collection classes only store objects right? Whats the point in declaring a type for them? Also can someone explain the following:
Set<Integer> aSet;
aSet = new HashSet();
aSet.add(1);
aSet.add(2);
aSet.add("boobs");
System.out.println(aSet);
That dosent let me put in a string and dosent compile.
Set aSet;
aSet = new HashSet<Integer>();
aSet.add(1);
aSet.add(2);
aSet.add("boobs");
System.out.println(aSet);
This does let me put in a string and compiles fine. What gives?
Is declaring types for readability reasons and to prevent stuff that's not supposed to be in the set from being put in?
