In the case of
The object is copied.Code:Object obj1; Object obj2; obj1 = obj2;
However in the case of
p_obj2 is assigned the address of obj, thus it points to that object.Code:Object obj; Object *p_obj2 = &obj;
However, you have to realize that java uses something called reference counting. It will automatically release memory for you when it goes out of scope.
ie in Java
When you call method() a new Integer object is created and x is made to point to it. Then x is made to point to a different Integer object which hold the value of 2. Since the old Integer object is no longer referenced it can never be recovered, thus the JVM garbage collector will delete and re-use the memory where the Integer object that has a 1 is located.Code:public void method() { Integer x = new Integer(1); x = new Integer(2); }
However, in C/C++ when you make a pointer point to another object you are expected to handle the old object. This can be an annoyance at first, but it's a bit of an advantage (performance wise) when you can decide when to delete the memory which was previously taken up by the object. In java there is no guarantee of when the garbage collector is going to be called.
In java everything is pass by reference (except for primitives, which is pass by copy). In c++ everything is pass by copy. So when you don't want to copy an entire object, you just pass a copy of it's address (which is a pointer).
We could go on and on about this all day.
However, whenever I have a problem I go to .... no not google, but wikipedia and search it. Google is always second.
Wikipedia has a great comparison of Java and C++. It's much more in depth than anyone here will feel like writing.
cs_student