Cloning Objects
By Bruce Eckel
By this time you are probably comfortable with the idea that when you're "passing" an object, you're actually passing a handle (a reference). In many, if not all, programming languages, you can usually pass objects around the "regular" way and everything works fine. But there always comes a point where you must do something irregular, and suddenly things get a bit more complicated (or in the case of C++, very complicated). Java is no exception, and it's important to understand exactly what's happening with your object handles as you pass them around and assign to them. This and next month's columns will provide that insight.
Another way to pose the question, if you're coming from a pointer-equipped programming language, is, Does Java have pointers? Some have claimed that pointers are hard and dangerous and therefore bad, and since Java is all goodness and light and will lift your earthly programming burdens, it cannot possibly contain such things. However, it's more accurate to say that Java has pointers; indeed, everything that has a name in Java is one of these pointers, but their use is very restricted and guarded by both the compiler and the runtime system. These are what I've been calling handles, and you can think of them as "safety pointers," not unlike the safety scissors of early elementary school. They aren't sharp, so you cannot hurt yourself without a very great effort, but they can sometimes be slow and tedious.
Passing Handles Around
It's worth performing an experiment to show that when you pass a handle into a method, you're still pointing to the same object.