Pointers in Java

In Chapter 3 of the Sams book came my first big surprise. Maybe that’s an overstatement, but I thought this was at least worth noting and opening up for discussion. All along every tutorial I’ve visited has lauded Java because it “doesn’t have pointers.” I suspected all along this was bs and Ch. 3 confirms.

What’s surprising is that not only does Java have pointers, but if I understand things correctly, they are used rampantly. Virtually every object used is controlled as a pointer (called a “reference” in Java) behind the scenes. Granted, a Java programmer does not specifically declare pointers but given that all these objects are treated as such then isn’t the statement “Java doesn’t have a pointers” a bit misleading.

I have yet to think through all the implications, but one obvious example presented in the book is that trying to compare two objects with the ‘==’ operator will only yield true if they are in fact the identical object because it just checks the memory address and not the contents. This also explains why one can’t overload this operator as one could in C++. I find this aspect of the language interesting to say the least.

This entry was posted in learning java. Bookmark the permalink.

1 Response to Pointers in Java

  1. GregJ says:

    First, at least with the assumption that C# and Java are similar in this regard, as they are in so many others, there are quite simple ways of comparing the values of these objects without checking that they are, in fact, the same object. In C#, you use a method called “Equals(obj x)”, in this form
    myObject x;
    myObject y;
    if (x.Equals(y))
    {Do whatever;}
    Alternately, you can do
    Object.Equals(x, y); which will return whether x and y (any two objects) are equal.
    This might seem like a little more work, but in most cases you don’t even have to do that. While many “classes” are reference types (meaning they are referenced sort of like you reference things with pointers), most of the base types are “value types”, meaning they are not. That is, the VALUE of the object is what is referenced, not the abstract object.
    While this all is sort of reminiscent of pointers, it is not anywhere near as bad and sloppy and confusing, in my opinion. First of all, an enormous number of the errors introduced in C and C++ programming are by people getting screwed up somehow and inadvertently referring to the address of some object when they really want the object, or vice versa. Or accidentally overwriting an object when they meant to overwrite a local copy of it. Pointers are freakin’ whack. C# (and I believe Java) don’t let you do stuff like dereference pointers (you never need to know the “address in memory” of anything), as far as I know. You can pass around the reference to something, yes, but when it gets there, you can either treat it like the type of thing that it is, or else you can’t really do anything with it. You can’t just grab it’s address in memory and accidentally start doing math with it, for example, when you meant to grab some incoming int.
    Anyway, it’s sort of true that “reference types” are related to pointers, in that rather than being passed wholesale around the system (creating unnecessary overhead and multiple copies of stuff) they simply pass a reference (read: pointer) to the object. But it’s far less prone to getting screwed up, since you don’t do any of it manually, you don’t ever look at the memory address, and strong typing won’t let you start treating an object of the wrong type (in most cases) unless you explicitly say you want to do so (explicit casting).
    Also, when you’re creating your classes, you can usually specify that you want the type to be passed by value instead of by reference if you don’t want to worry about this kind of thing (i.e. if your program wants to just spew out output but not let anybody fuck with the internal objects you are using to create that output). In C# this can be done by using structs instead of classes, for example, but I think you can also explicitly take classes and say that you want to pass them by value, not by reference.

Leave a Reply

Your email address will not be published. Required fields are marked *