How Java handles objects
How Java handles objects had always been a thorny issue for me. I don't know if it was a case of never being taught properly or my personal inability to learn, but I either way by the time I began my professional development career I only had very loose understanding of how Java handles objects. This gap in my knowledge impacted my ability to be an effective developer; my code had more bugs and implementing changes was more difficult. I remember many of my friends back in school also having trouble here, so I will spend my first real post (yay!) covering this important subject.
All object variables are pointers
Probably one of the first things you learned, or at least heard, about Java, was all object variables are pointers, but what does this mean and what are the implications? In simplest terms it means that a variable that holds an object doesn't actually store the value of the object, but its location in memory. Take for example the code snippet below:
String name = “Bob”;
The variable “name” doesn't hold the value “Bob,” but the location of where “Bob” is stored in memory. It's a difficult concept to grasp, I hope to make it easier to understand as well as go over the significance of this characteristic below.
The implications
So an object variable holds reference to an address in memory, but how will it effect your day to day programming? Well probably the first problem every programmer runs into when working with objects is comparing two Strings using the “==” operator instead of “.equals().” Using the “==” operator doesn't work as excepted on Strings (or any object), because like stated previously object variables do not contain the value of an object, but the address to where the value is stored. So when you are performing this operation: (name == “Bob”), you are actually comparing the the addresses in memory of the two objects, not their values. Whereas with name.equals(“Bob”) you are retrieving the value stored at the memory address the name variable references and comparing it to the String object passed into the equals(String) method. So while comparing two strings, or any other object requires more work than comparing two primitive data types like int or char there are some advantages.
Passing by reference can often be used in place of returning
Before I fully understood objects, if I wanted to use a method to manipulate an object I would often have that method returning a value. An example of this:
public class ObjectExample {
public static void main(String args[]){
Person a = new Person()
a = changeFirstName(a);
System.out.println(a.getFirstName());
}public static Person changeFirstName(Person a){
a.setFirstName("Joe");
//Prints Joe
return a;
}
}
While there is technically nothing wrong with this behavior, it is not the most efficient or readable way of handling this scenario. Also as you will likely notice in my next example the return statement is largely redundant. So here is an example of passing by reference:
public class ObjectExample {
public static void main(String args[]){
Person a = new Person();
changeFirstName(a);
System.out.println(a.getFirstName());
//Prints Joe
}public static void changeFirstName(Person a){
a.setFirstName("Joe");
}
}
This code prints the name “Joe”, because remember you are not passing the value of the object, but its address in memory. So the method changeFirstName(Person) is manipulating the value stored at the address of the Person object passed in.
Remember
When you assign an object variable to another object variable you are not copying the value of the assigner variable to the assignee, but the reference. What this means is when you change the value of either variable, you change the value for both variables as shown in the example below:
public class ObjectExample {
public static void main(String args[]){
Person a = new Person();
Person b = a;a.setFirstName("Joe");
System.out.println(a.getFirstName());
//prints Joe
System.out.println(b.getFirstName());
//prints Joe
}
}
That sums up a fairly basic overview of how Java handles objects. If you have any questions or comments please leave a comment and I will try to respond as best I can.
Related posts:
