So i've read this link: here. But i'm still not 100% clear on the topic.
If i had an arbitrary collection say:
public Set<Integer> hello () {
Set<Integer> hi = new HashSet<Integer>();
hi.add(3);
doWork(hi);
return hi;
}
static void doWork(Set<Integer> set) {
set.add(1);
return;
}
would athis.hello()
call return [1]
or [1, 3]
What if instead we did something like:
public Set<Integer> hello () {
Set<Integer> hi = new HashSet<Integer>();
hi.add(3);
doWork(hi);
return hi;
}
static void doWork(Set<Integer> set) {
Set<Integer> temp = set;
temp.add(1);
return;
}
Also, since I'm still not entirely clear on the difference between how it passes primitives and objects - does the fact that it's a set of integers (a primitive type) effect anything? (I suppose that a Set is an Object so this isn't an issue with Collections)
What if we had something even a little more complicated like
Class Dog {
String name;
Dog(String name) {
this.name = name;
}
public void setName(String newName) {
name = newName;
}
}
public Set<Dog> hello () {
Set<Dog> hi = new HashSet<Dog>();
hi.add(new Dog("Woof");
doWork(hi);
return hi;
}
static void doWork(Set<Dog> set) {
Set<Dog> temp = set;
for (Dog d : temp)
d.setName("Bark");
d = new Dog("Ruff");
d.setName("Charles");
return;
}
What would the dog's name be, and what happens to the other dog names?