#10 Reference and Value Types

In this tutorial I’m going to explain something that is important to understand when programming and that is the difference between a reference type and a value type.

So to get started let’s create a new class in the default package by clicking selecting new class and I’m going to name the class ReferenceAndValueType and click Finish and let’s create our main method by typing the word main with some control spacebar and hitting enter.

I’ll start off by explaining a value type. Now all the primitive types in Java are value types and they include:

//byte, short, long, float, double, boolean and char.

Anytime you’re using a variable that is of any of these types you’re using a value type.

And anytime you’re using a variable that is of a different type such as string or a person or a label, you’re using what’s called a reference type.

Now I’m going to try to explain what the difference is.

Say you create an integer variable named X.

You could think of something like this happening in memory.

You get this box and you call about X and now you could say something like X equals 5.

And that would be putting the value 5 inside of the box.So this is a value type.

Now if you’re going to be passing x around say you’re going to give it to a method and you want your method to do something with that value what you’re actually passing is the value 5 you are not passing the variable x containing 5.

You are only passing the value 5.

p121.jpg

So as an example let’s say we have X equals 5.

And now let’s create a method that will expect an integer and see what happens when we try to change that value. So let’s say a sstatic void addOneTo(int number) and inside of the method we’ll say number = number + 1.

And now we’ll call that method using our x variable, we’ll say add 1 to x and now will print out the value of x. And what do you think it should be.

Well let’s run it

p122.jpg

and see we’ll see the value printed out as 5. So what happened here is because integers a value type. We’re not passing the x variable in. So we’re not actually operating on that variable here.

We’re operating on the value of that variable just 5 now reference types are different.

When you create a reference type let’s say following the example from our last tutorial we create a person named John.

p123.jpg

So we have this variable of type person called John which would look like a person John.

Now what happens when we say John equals new person

well when you create an object when you call the constructor with the keyword new you create that object somewhere off in memory.

So let’s say we’re creating our Person object and this is what it looks like.

p124.jpg

Person and what new person actually returns to you isn’t the object itself but it actually returns the address in memory where this object exists.

So what are John variable actually contains is a reference to our newly created person object.So when we call a method and pass in a reference type like John the value that we’re actually passing is an address that takes us to the object which means we can actually change the object itself.

Now this is the big thing to understand when we pass a value type we’re passing 5 which means essentially we’re passing a copy of the value when we pass the reference type we’re passing a reference.

p125.jpgThat takes us to the object which means that we are able to manipulate the object itself.

So as an example in code let’s create a method that takes our John variable let’s say first set setting John’s age to 20 and now we’ll create a method called celebrate birthday of John and we’ll let eclipse create that method for us.

p126.jpg

And now inside celebrate birthday we’re going to say John that said age John.getAge() +1

So we’re setting John’s age to John’s current age plus one.And now we were to print the value of John.getAge and run the application will see that:

p127.jpg

John is now 21 years old.

p128.jpg

In other words we were able to manipulate the values inside of John object because what we’re passing is a reference to that object. So hopefully you’re kind of getting it.

Here we’re creating index which creates that box. We’re putting the value 5 inside of that box. And here we’re adding one to X because X is a value type. We’re simply passing the value 5 into that method.

p129.jpg

So X remains unchanged.

Where in this case we’re creating a person John which is a reference type, and we’re assigning to that box in memory a reference to a new person object and we are passing a bad reference to the object into our celebrate birthday method and inside of that method we’re passing it a reference to a person and

we’re using that reference to change the values inside of the object.

So let’s change five back to X and we should probably put in some comments.

So we call this method

// we’re passing a value type so that we pass a copy of the value of x which is 5

and inside of the add 1 to method we

// manipulate a copy of the value passed.

And here

// we are creating a new person object and assigning John to refer to it.

And here 

//we are passing a reference to a method, o the method can use that reference to manipulate the object.

p130.jpg

So now you should understand the difference between a reference type and a value type.

Thanks for watching.

Leave a comment