views:

1762

answers:

6

This is a Java related question. If I create a new HashMap and a new List, and then place the List inside the Hashmap with some arbitrary key and then later call List.clear() will it affect what I've placed inside the hashmap? The deeper question here being: When I add something to a hashmap, is a new object copied and placed or is a reference to the original object placed?

Thanks, Diego M

+3  A: 

Java is pass-by-reference-by-value.

Adding the list to the hash map simply adds the reference to hash map, which points to the same list. Therefore, clearing the list directly will indeed clear the list you're referencing in the hashmap.

James Camfield
aaaaaaahhhhh!!! No!!!! Java is strictly pass-by-value! See http://javadude.com/articles/passbyvalue.htm
Scott Stanchfield
We know. Java is pass by value. However, saying that can be confusing. Java acts like it is pass by reference.
jjnguy
Nope - Java does not act that way at all. It acts exactly like C does (which is also pass-by-value)
Scott Stanchfield
Exactly. There no harm in seeing it as pass-by-reference, even if it does it by passing values (which is what the reference is anyway)...
James Camfield
There's a huge harm in it. Pass-by-reference semantics allow you to do things like creating a swap method (see my article that I ref in my first comment to this answer); you cannot do that in Java.
Scott Stanchfield
Well, it seems like it acts like pass by reference, because, people don't see the 'pointer' that is being passed around. It is not an easy topic to wrap ones head around. Lots of people don't know C.
jjnguy
Yep, it's not easy, which is why I wrote that article. I'm a compiler guy and a teacher; as a compiler guy, the difference is critical to implementation. As a teacher, the difference is critical to proper understanding of the language, especially why you cannot write something like swap(x,y) or have output/variable parameters.
Scott Stanchfield
I don't think it is helpful to argue about it. I like abstracting out the details and thinking about it in the bigger picture. I see where you are coming from and most definitely agree that people 'should' know how it works behind the scenes. But that doesn't mean that they have to know.
jjnguy
It's very worth arguing, because it's spreading misinformation and causing confusion with regard to how you can get data out of a method, like why if you have foo(Dog d) and write d = new Dog() inside foo, the dog pointer on the outside does not change.
Scott Stanchfield
I think pass-by-reference-by-value is actually a pretty fair representation of the true semantics. Or would you prefer pass-pointer-by-value?
Michael Myers
I wouldn't mind "pass pointer by-value" (note where the hyphen is - when you hyphenate it tends to be looked upon as official jargon). The terms "pass-by-value" and "pass-by-reference" have very specific (and important) meanings. It's really easiest to think of by separating the concepts: 1) Java has pointers 2) the value of a pointer is the target object's location 3) when you pass an arg you're passing a value 4) the target location gets passed/copied to the formal parameter. Thus you end up with the formal param pointing at the same object as the arg.
Scott Stanchfield
I don't see what's so hard to get your head around, really. It's a very distinct difference, and the only sticking point is that Sun decided to call pointers "reference values". They mention in the JLS that a reference value is a pointer to an instance or array (4.3.1), and make it pretty clear that method parameters are storing *values* (4.12.13). Thus, Java has pointers, and is pass-by-value.
Adam Jaskiewicz
+4  A: 

What's happening here is that you're placing a pointer to a list in the hashmap, not the list itself.

When you define

List<SomeType> list;

you're defining a pointer to a list, not a list itself.

When you do

map.put(somekey, list);

you're just storing a copy of the pointer, not the list.

If, somewhere else, you follow that pointer and modify the object at its end, anyone holding that pointer will still be referencing the same, modified object.

Please see http://javadude.com/articles/passbyvalue.htm for details on pass-by-value in Java.

Scott Stanchfield
Aaahhhhhhh! there's no such thing as pointers in Java...
James Camfield
Sure there is. Why do you think there's a "new" operator. Please read my article ref'd above - it'll help you understand what Java is really doing. Just because java does not implement pointers the same way C/C++ does doesn't mean it doesn't have pointers. Pascal implemented pointers without artithmetic on them...
Scott Stanchfield
Would you like to take this opportunity to make *another* link to your article?And maybe there is, but the programmer can never see them or use them in Java, so I fail to see the need to talk about them when all we use are References.
James Camfield
There can never be enough links to it... (I put it in comments to the above answers b/c they were posted first - many people won't read past the first couple answers and simply upvote them). The programmer can very well see the difference -- swap(x,y) and output/variable parameters are two very important language features that exist in some languages (C++, Ada, Pascal for example) but not in Java.
Scott Stanchfield
I'm not really clear why people are disagreeing about this. Isn't this written somewhere final? The different opinions are confusing.
Diego
It's very clear in the Java Language Specification: http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.1: "When the method or constructor is invoked (§15.12), the values of the actual argument expressions initialize newly created parameter variables".
Scott Stanchfield
@Diego There are some terminology issues. If you look at the accepted terms, though, rather than what Sun "officially" calls them, it's perfectly clear what is going on. Sun defines the meanings of their names for things by using the accepted terms, in that they explicitly state that a reference *is* a pointer (JLS 4.3.1).
Adam Jaskiewicz
People see "reference" and get all confused. That Sun decided to call pointers "reference values" gets "pass-by-reference" into peoples' heads.
Adam Jaskiewicz
A: 

Geeze people...everything in Java is pass by value. When you pass an object, the value you are passing is the object reference. Specifically, you are passing a copy of the object reference. There are no pointers in Java either, though references are similar. Get it right!

GreenieMeanie
There are pointers in Java. What do you think "Dog d;" defines? If it's actually an Object, why can't you ask it to bark without pointing it: "d = new Dog();"?
Scott Stanchfield
BUT THEY AREN'T CALLED POINTERS - they are called REFERENCES. That's the whole damn point.
GreenieMeanie
Using the word "pointers" in Java is misleading (and NOT the correct term), giving one the impression of C-style pointers. References are not the same (and you can't manipulate references the same as "pointers"), so let us please refrain from that term in Java.
GreenieMeanie
By that logic, Pascal and Ada do not have pointers either (they both do, by the way). Not all languages allow pointer arithmetic. C/C++ were not the first languages to use pointers. You can maipulate pointers in Java the same ways you can in many langauges (assign, dereference).
Scott Stanchfield
But in Pascal and Ada, they are also called "pointers" if I recall correctly. They are not called pointers in Java.
GreenieMeanie
A rose by any other name would smell as sweet... The term "reference" in language design is similar to an alias - you're using more than one name to refer to the same variable. This is actually quite different from what Java does. Sun chose to call them references (a very poor choice, and many agree) as basically PR - they wanted to bill Java as a safe language; "no pointer arithmetic" is a big selling point, and they went overboard. "Dog d" in Java acts exactly the same as "Dog *d" in C++ (with the exception of no arithmetic)
Scott Stanchfield
@GreenieMeanie From the JLS, 4.3.1: "The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object."
Adam Jaskiewicz
One more note: NullPointerException is a really good example of how they were thinking before the PR machine started grinding. They thought of the concept as pointers, but late in the game decided to change the name and missed a spot...Note that there are language design concepts, and there are specific language designers' namings. Anyone can write a language. And unfortunately, some make naming mistakes.
Scott Stanchfield
@Adam Thanks for pointing out that quote - I hadn't noticed that before
Scott Stanchfield
-1 java does have pointers, it doesn't have pointer arithmetic.
wds
@Scott, quite agree, reference types are pointers passed by value, hence we have the NullPointerException for when the pointer is not pointed at a live object. Just because they can't be manipulated except by assignment does not mean they aren't pointers, it just means they are more tightly controlled than C/C++ etc. pointers.
Geoff
A: 

When I add something to a HashMap, is a new object copied and placed or is a reference to the original object placed?

It is always a reference to the object. If you clear the HashMap the object will be still "live". Then the object will be destroyed by the garbage collector if no one is referencing it anymore. If you need to copy it, take a look to Object.clone() method and to the Cloneable interface

daitangio
A: 

Generally you always deal with references in Java (unless you explicitly create a new object yourself with "new" [1]).

Hence it is a reference and not a full object copy you have stored in the map, and changing the list will also effect what you see when going through the map.

It's a feature, not a bug :)

[1] Puritans will include "clone()" and serialization, but for most java code "new" is the way to get objects.

Thorbjørn Ravn Andersen
Did you mean "purists"? I've got a picture of the Quaker Oats guy in my mind right now...
Scott Stanchfield
Whatever :) Me issanot native Englissa speeka.. :-D
Thorbjørn Ravn Andersen
A: 

Try it out

package test32;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

class Foo {
    public Foo(int id, String name) {
 this.id=id;
 this.name=name;
    }

    public static void main(String[] args) {
 HashMap strs = new HashMap();

 // create a list of objects
 List ls = new ArrayList();
 ls.add(new Foo(1, "Stavros"));
 ls.add(new Foo(2, "Makis"));
 ls.add(new Foo(3, "Teo"));
 ls.add(new Foo(4, "Jim"));

 // copy references of objects from list to hashmap
 strs.put("1", ls.get(0));
 strs.put("2", ls.get(1));
 strs.put("3", ls.get(2));
 strs.put("4", ls.get(3));

 System.out.println("list before change  : " + ls);
 System.out.println("map before change: " + strs);
 // get an object from the hashmap
 Foo f=strs.get("1");
 // set a different value
 f.setId(5);
 // observe that the differences are reflected to the list and to the hashmap also
 System.out.println("list after change  : "+ls);
 System.out.println("map after change: "+strs);
    }

    private int id;
    public void setId(int id) {
 this.id=id;
    }
    public int getId() {
 return this.id;
    }

    private String name;
    public void setName(String name) {
 this.name=name;
    }
    public String getName() {
 return this.name;
    }

    public String toString() {
 StringBuilder sb = new StringBuilder();
 sb.append(id);
 sb.append("-");
 sb.append(name);
 return sb.toString();
    }
}
stavros