Skip to content
loading...
Discussion
markdown guide
 

Hopefully you're not too young to remember this, but before the days of high-speed internet, software was more commonly distributed on CDs or floppy disks rather than downloaded from the internet. In those days, if you wanted to borrow a friend's video game, you would meet up, they would give you their physical CD with the game, and you'd stick it in your computer and play. This is like the "reference type" where the game you are playing is the exact same game that your friend had. If you happen to scratch or otherwise destroy that CD, it is destroyed completely and neither you nor your friend can play that game anymore, because it was the same object. In this case, the CD is the reference to the game.

A "value type" would be more like downloading an image of that game from the internet. Rather than your friend giving you his physical CD, you just download a copy of that game. Now you can do whatever you want with that copy, but those changes will never be reflected in the original source. Because you do not have direct access to the source of the game, but instead what you have is the game, it is a value type.


There are several languages which allow the programmer to explicitly decide whether to pass data by "reference" or "value", such as C++ and PHP, and there are tradeoffs to both. Passing by reference is really fast because the runtime does not have to create a copy of the object, but if anyone else changes your object in ways you didn't anticipate then your code might break. Passing by value makes a full copy, which is slower but prevents other code from changing yours unexpectedly.

Other languages, like Java, do not allow the programmer to make this distinction. Rather, Java automatically passes all Objects by reference, and all primitives (int, double, byte, etc.) by value.

 

I think this is a good answer, but the terminology around how Java passes arguments may be slightly incorrect: In Java variables for object types are always references. We can think of them as being arrows that point to the actual values. When we pass such a variable to a function as an argument, Java creates a copy of the reference. So now there is another arrow inside the function that points to the same value. In other words, these references are passed by value. As far as I know, Java does not have objects as value types.

 

Good point, that's exactly what's going on with the Java runtime. And it's a good distinction to point out as that behavior is how the garbage collector knows when to clean up objects: when there are no more references pointing to an object, its memory can be freed.

That being said, this is all transparent to the programmer writing Java code, so while there may be multiple references under the hood, to the developer it just looks like a single shared object. You'd never really notice the difference in reference and value types in a local scope, but the behavior of these different types as they are passed between methods becomes significant and important to understand.

 

I’ll take a stab at this...

  • You want to read a book. In your room there’s a box. You look inside the box and there is a book. You start to read the book. In this case we can think of the box as a “value type.” It directly contains the thing you’re interested in.

  • This time the box just has a card in it. The card tells you where to find a given book on the bookshelf. In this case the box is a “reference type.” It tells you where the thing you want can be found but it’s not something that you need on its own.

You can think of any variable as a box, i.e. a location in memory. If that location contains an actual value of interest, then in this sense it can be considered a value type. If it points to another location in memory, then it’s a reference type. In this sense, things that take more than one memory location, like strings or arrays, are generally reference types by default.

However, I think that value type semantics can be applied to any type. It would mean, for example, that something like a = b = SOMETHING would create a copy for a and changing that value would not affect b.

Classic DEV Post from Jul 30 '19

PublishTo.Dev: Scheduling article publishing on dev.to

camilla profile image
I love refactoring.