Bypassing Copy Constructor
2 replies to this topic
Members - Reputation: 141
Posted 15 February 2012 - 06:28 AM
I'm going to be learning Java soon, so I was wondering about the parameters for functions. In C++ I always used pointer when I wanted to directly affect a variable or object from a function. I've heard Java doesn't have pointers, so how will I be able to do this?
Members - Reputation: 1363
Posted 15 February 2012 - 06:35 AM
In Java, all objects are references and exist on the heap. You don't have the choice of constructing objects on the stack or on the heap as you do with C++. If you want to actually perform a deep copy of an object rather than simply creating a secondary reference to it, you have to explicitly clone the object. Also keep in mind that when you pass an object to a function, the reference is passed by value.