2

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?

Community
  • 1
  • 1
gadu
  • 1,816
  • 1
  • 17
  • 31

6 Answers6

2

It will return [1, 3] because you are passing the same set.

In the 2nd case it will again return [1, 3], because the same object is referenced.

In short: you have the same object passed around, and you modify its state (in the case of a collection, the state is the elements inside the collection)

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
1

It will return [1,3]. Try printing out the collection with System.out.println(Set)

Because, after you created the Set hi inside the hello function and added 3. You have passed the same Set to doWork(Set) function, which add 3 to the passed Set, as java passes object reference(the reference of instance of Set) by value.

Sage
  • 15,290
  • 3
  • 33
  • 38
0

it will return [1,3], your are making change in the same object

0

Both cases will return [1,3].

Even creating the temporary variable Set<Integer> temp = set; you are calling a method that sets a value for temp and set, because they refer the same object.

Manuel Pires
  • 637
  • 3
  • 19
0

In both senarios, when doWork(hi) is invoked, you are assigning the argument hi to the parameter Set<Integer> set, similar to the following:

Set<Integer> set = hi;

What does that mean? it means that both set and hi are now referring to the same object in the memory:

-------
| set | ----> [HashSet instance]
-------               ^
                      |
-------               |
| hi  | ---------------
-------

In the first scenario, when set.add(1) is invoked, a 1 is added to the set via the reference set. Now hi, can see that 1 as well since it has a link to the same object in the memory.

In the second scenario, you create a third reference temp by this assignment:

Set<Integer> temp = set;

Now, all the three references are referring to the same object in the memory:

-------                             --------
| set | ----> [HashSet instance] <--| temp |
-------               ^             --------
                      |
-------               |
| hi  | ---------------
-------

and when you add 1 to the set via temp:

temp.add(1);

this will be reflected to the other 3 references.

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
0

First of all, "objects" are not values in Java; hence you cannot "pass" an "object". The only types in Java are reference types and primitive types. Passing works the exact same way for all types, reference types and primitive types. References are pointers to objects.

Collections in the Collections Framework are designed to be collections of references only. This is because to have collections of primitives, it would need to be implemented separately for each primitive type, and separately from reference types. However, one implementation works for all reference types because they are all subtypes of Object. Set, List, Collection, etc. are all collections of references. You have a Set<Integer> which is a collection of Integer values; Integer is a reference type, not a primitive type. Also, only a reference type can be a type argument in generics (what goes in the < >). You may be confusing it with int, which is a primitive type. All primitives types have a corresponding "wrapper" class; Integer is the one for int.

So Set is a reference type and hi, set, etc. are reference variables. You pass a reference to another function, and Java is pass-by-value always, so inside the function set is a copy of the passed reference; but of course it points to the same object as the passed reference. If it makes changes to the object using that reference, the reference in the original scope can see it.

newacct
  • 119,665
  • 29
  • 163
  • 224