You manipulate objects with handles

Bruce Eckel's Thinking in Java Contents | Prev | Next

with handles

Each 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 special syntax?

All this is simplified in Java. You treat everything as an object, so there is a single consistent syntax that you use everywhere. Although you treat everything as an object, the identifier you manipulate is actually a “handle” to an object. (You might see this called a reference 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.

Also, 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 String handle:

String s;

But here you’ve created only the handle, not an object. If you decided to send a message to s at this point, you’ll get an error (at run-time) because s isn’t actually attached to anything (there’s no television). A safer practice, then, is always to initialize a handle when you create it:

String s = "asdf";

However, this uses a special case: strings can be initialized with quoted text. Normally, you must use a more general type of initialization for objects.



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds