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: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds