|Bruce Eckel’s Thinking in Java||Contents | Prev | Next|
programming language has its own means of manipulating data. Sometimes the
programmer must constantly be aware of what type of manipulation is going on.
Are you manipulating the object directly or are you dealing with some kind of
indirect representation (a pointer in C or C++) that must be treated with a
this is simplified in Java. You treat everything as an object, so there is a
single consistent syntax that you use everywhere. Although you
as an object, the identifier you manipulate is actually a “handle”
to an object. (You might see this called a
or even a pointer in other discussions of Java.) You might imagine this scene
as a television (the object) with your remote control (the handle). As long as
you’re holding this handle, you have a connection to the television, but
when someone says “change the channel” or “lower the
volume,” what you’re manipulating is the handle, which in turn
modifies the object. If you want to move around the room and still control the
television, you take the remote/handle with you, not the television.
the remote control can stand on its own, with no television. That is, just
because you have a handle doesn’t mean there’s necessarily an
object connected to it. So if you want to hold a word or sentence, you create a
here you’ve created
the handle, not an object. If you decided to send a message to
at this point, you’ll get an error (at run-time) because
isn’t actually attached to anything (there’s no television). A
safer practice, then, is always to initialize a handle when you create it:
s = "asdf";
this uses a special case: strings can be initialized with quoted text.
Normally, you must use a more general type of initialization for objects.