See Ceiling Cat.
Objects and Pointers: Pointer confusion
Here’s another little tutorial that most amateur developers need to take the time to read through. When it comes to objects and pointers in Java people seem to get confused without the explicit *’s and &’s that most C-based languages offer. New developers seem to often lose track of whats a pointer and whats a value when programming in Java and therefore end up with lot’s of unexplained behavior/bugs or exceptions in their code.
Below is a series of tests which demonstrates most (if not all) the scenarios where pointer confusion takes place. The following should at least solidify this basic concept for Java programmers to a degree of comfort that pointer confusion is no longer an issue. Enjoy:
Here is a class, Something, that is composed of another class, SomethingElse.
Something.java:
public class Something { private SomethingElse somethingelse; public SomethingElse getSomethingelse() { return somethingelse; } public void setSomethingelse(SomethingElse somethingelse) { this.somethingelse = somethingelse; } public void print() { somethingelse.print(); } }
Here is the SomethingElse class that Something is composed of:
SomethingElse.java:
class SomethingElse { public int n = 0; public void print() { System.out.println(n); } }
Finally, here is the Main class which contains out main() method with all our tests. The tests purposes are identified in the comments:
Main.java:
public class Main { public static void main(String[] args) { SomethingElse a = new SomethingElse(); SomethingElse b = new SomethingElse(); SomethingElse c = new SomethingElse(); a.n = 1; b.n = 2; c.n = 3; Something something = new Something(); something.setSomethingelse(a); System.out.print("Value of something before any modifications to a: "); something.print(); /* * Let's try assigning a new value to a in the Main class to see if it * affects the value held within the something object. */ System.out.println("==Test 1=="); System.out.print("Value of a before modification: "); a.print(); a.n = 100; System.out.print("Value of a after modification: "); a.print(); System.out.print("Value of something after first modification of a: "); something.print(); /* * Let's try assigning the pointer in b to the pointer in a then changing b and printing out a. */ System.out.println("==Test 2=="); System.out.print("Value of a before modification: "); a.print(); b = a; b.n = 25; System.out.print("Value of a after modification of b: "); a.print(); System.out.print("Value of something after first modification of b: "); something.print(); /* * Let's try assigning the pointer in a to the pointer in c. */ System.out.println("==Test 3=="); System.out.print("Value of a before modification: "); a.print(); a = c; System.out.print("Value of a after modification: "); a.print(); System.out.print("Value of something after second modification of a: "); something.print(); /* * Let's try setting the pointer in a to null. */ System.out.println("==Test 4=="); System.out.print("Value of a before modification: "); a.print(); a = null; System.out.println("Attempting to print a after nullifying it's pointer will throw a NullPointerException so let's move on."); System.out.print("Value of something after third modification of a: "); something.print(); } }
Output:
Value of something before any modifications to a: 1 ==Test 1== Value of a before modification: 1 Value of a after modification: 100 Value of something after first modification of a: 100 ==Test 2== Value of a before modification: 100 Value of a after modification of b: 25 Value of something after first modification of b: 25 ==Test 3== Value of a before modification: 25 Value of a after modification: 3 Value of something after second modification of a: 25 ==Test 4== Value of a before modification: 3 Attempting to print a after nullifying it's pointer will throw a NullPointerException so let's move on. Value of something after third modification of a: 25
So to explain what’s happening here.
In the first test we’re testing to see whether changing the value of a in the Main class will affect the value within something’s somethingelse class because it holds a pointer to the same memory space that a does. The test does change the value of a both within the Main class and the value of n within something’s somethingelse class.
In the second test were attempting to show how you can assign object pointers and alter the same memory space through different object pointers. We’re doing this by assigning b the same pointer held by a and then changing b and printing out the value of a which prints out the new value that we just assigned b. This happens because when we assign b the pointer held by a we have assigned both a and b to point to the same memory space. When we alter b we are altering the data in that memory space and when we go to look at it through a we are still looking at the same memory space we just altered with b. Get it? Got it? Good.
The third test is to show the opposite of test two where something’s somethingelse class holds a reference to the memory space held by Mains a object pointer but then we assign a the same pointer held by c thereby losing our pointer from a to the same memory location as something’s somethingelse class. This test is to show that assignment of objects with the ‘=’ operator only works on pointers so after we do this something’s somethingelse class doesn’t take on the value of c we just effectively eliminate our pointer through a to the same memory space held by something’s somethingelse class.
The last test is just to reinforce the same concept the previous three were trying to demonstrate. We eliminate the pointer held in a so if we try to access it the code would throw a NullPointerException in the Main class but something’s somethingelse class still holds the reference to the original memory space and the value printed remains the same.
I hope this demonstrates enough examples of object pointers to clear up some of the confusion. There are a number of other things I could have shown in this code but they would have been just as redundant as the fourth test in the code already provided.
Keep coding!
Pass by what?
Here is a problem I hear over and over again from developer entering the Java realm. Many new developers have either never been introduced to the concept of pass-by-value/pass-by-reference or they just never understood what it meant. So I threw together some simple source code to demonstrate this basic concept that every developer of ANY language should know.
Here’s a simple object class that has only a string.
Something.java:
public class Something { String name; }
Here are two objects, one to change the name attribute in the Something class, and one to change a primitive type, by simply changing an int to 1.
ObjectChanger.java:
public class ObjectChanger { public void changeSomething(Something s) { s.name = "changed"; } }
PrimitiveChanger.java:
public class PrimitiveChanger { public void change(int n) { n = 1; } }
Now let’s look at the Main class which contains this projects main() function.
Main.java:
public class Main { public static void main(String[] args) { // Let's try changing an object type Something s = new Something(); s.name = "unchanged"; System.out.println("String before pass: " + s.name); ObjectChanger oc = new ObjectChanger(); oc.changeSomething(s); System.out.println("String after pass: " + s.name); if(s.name.equals("unchanged")) { System.out.println("Objects are passed-by-value."); } else System.out.println("Objects are passed-by-reference."); // Now let's try changing a primitive type int n = 0; System.out.println(n); PrimitiveChanger pc = new PrimitiveChanger(); pc.change(n); System.out.println(n); if(n == 0) { System.out.println("Primitives are passed-by-value."); } else System.out.println("Objects are passed-by-reference."); } }
And finally the output:
String before pass: unchanged String after pass: changed Objects are passed-by-reference. 0 0 Primitives are passed-by-value.
As you can gather by reading through the code and output when an object is passed into a function it gets passed-by-reference meaning if that function changed that object within it’s logic then the original object will reflect those changes. A primitive type on the other hand is passed-by-value which means it basically gets a copy of the value to take away and do as it will with it. The called function can manipulate the passed in primitive value as much as it wants and the original value outside of the called function will remain the same as it was before it was passed to the function.
I hope this helps to clarify some of the misunderstanding surrounding this topic. This example does not take into account the overloading of the clone() function in your objects and then passing a reference to an objects clone rather than the original objects itself by using the obj.clone() syntax. Getting into the clone() function however deserves a whole other post.
Some things to look forward to
A long long time ago in a sub domain not so far away a couple friends and I ran a blog called WAFN. On this blog we explored a very big range of topics. The mistake we made however was trying to provide news even though we were not really plugged into that universe. After a recent conversation with a couple of other friends about starting a podcast we took a look at our old blog. After trying every credential set I could remember I was able to log into the admin page and we found that we still get an enormous amount of hits on that website. So there is talk about reviving WAFN perhaps under a different name but getting a similar group of people together to revive it.
The second project in talk is about the podcast our friends want to start which we might be involved with. As more news comes I’ll make sure to share but as it stands for now it is still in the planning stages. So stay tuned!

