Thursday, February 18, 2010

Analogies for Pointers in Computer Science

As part of an argument with an acquaintance for why C is better than Java, and why it is worth the hassle to use pointers, I quickly came up with a few analogies for pointers and why they are cool. My programming prof liked them, so I am providing them here for your enjoyment. If you don't know what a pointer is, see Wikipedia.

On using pointers:
  • +It's like texting your roommate to say "There's cake in the fridge if you want some" instead of actually bringing him/her the cake.
  • +It's like passing around a kite string, and then everybody gets to fly the kite without moving the actual kite at all, just the string.
  • +It's like Rickrolling in that you can pass around the same link with many names, and still end up in the same place.
If you have any more, you should totally add them in a comment!

(And if you want to use these for your class or tutorial, feel free, but please cite me!)

2 comments:

K Throbbs said...

I like the analogies. But don't forget that under the hood Java does have pointers. I get more null-pointer exceptions from Java than anything else. Any time you create an identifier for an object, you're really creating a pointer to an object. You just can't do it for base types (although you can wrap them in objects...). That's why you need a constructor. The object doesn't exist until you call it.

I'm sleepy, so this is the best analogy I can come up with: Let's say an array is like a neighborhood of houses, and you want to go to a specific one. In C, I can take you to the street you want and tell you how many houses to pass. In Java, I have to drive you straight to the house. C is much more useful in that sense. But it doesn't have garbage collecting, which is the only reason I can see for hiding pointers from the programmer.

Rae Botsford said...

I haven't touched Java in a while, and when I did, I'm sure if I got a null-pointer exception it didn't actually register with me that there was a pointer at stake somewhere. I would have just thought, "My code is broken. Why?"

I like your analogy!