This was originally posted to Facebook by me at 2010-08-02 23:20.
Note: For purposes of anyone interested in this, pointers in Java are more commonly referred to as references
, because everything in Java must have it’s own name (method vs. function). This makes operating the Google DuckDuckGo machine easier. I wish I had known that when I wrote this.
In Java there are a few `primitive' datatypes, the rest of the datatypes are implemented as classes. They are: byte short int long float double char <pointer> Normally, when declaring a primitive, you write the type of the primitive as the variable type. However, the reason I wrote pointer in brackets is that you DON'T write pointer when declaring a variable storing a pointer. For example, when I write java.util.Stack stack = new java.util.Stack(); The variable stack does NOT store an object. It stores a /pointer/ to an object. A pointer stores the memory address where a object is stored. ALL VARIABLES STORE PRIMITIVES, IT IS IMPOSSIBLE TO STORE AN OBJECT. For this reason, it is possible for the JVM to manage memory for you; it keeps track of the pointers, and once there are no more pointers pointing to a particular object, it can be deleted. Why does this matter? Well, consider that `String' is a class (`java.lang.String'), not a primitive. Consider the following code: String str1 = "foo"; String str2 = "foo"; return (str1 == str2); If String were a primitive, it would return true; but it returns false. Strings are a little complex because they contain syntactic sugar. Let's do the same example with an integer instead: Integer int1 = new Integer(5); Integer int2 = new Integer(5); return (int1 == int2); The `java.lang.Integer' class is a class wrapper around the `int' primitive. The `new' operator creates a class, and returns a pointer to it. The source code to the java.lang.Integer class contains the: private int value; public Integer(int val) { value = val; } So, the above example generates to objects belonging to the java.lang.Integer class. Even though the .value's of the two objects are the same, they are separate objects. Therefore, int1 and int2 store two separate memory locations. These two locations in the memory store the same data, but it is stored at both locations. The `==' operator takes two primitives, and compares their direct values. Since int1 and int2 store two different memory locations, `(int1 == int2)' evaluates to false.
Note: as pointed out by Ari Consul, everything following is false. The JVM does not copy the objects, it returns the pointer directly (no magic, as I’d suggested). The JVM will free() the object when there are no more pointers to it, via basic reference-counting
About copying objects: when returning a private pointer, the JVM makes a copy of the object that the pointer points to, and returns a pointer to the new object, NOT the original pointer. For example if I declare the method: public static Integer getInt() { Integer val = new Integer(5); return val; } It does NOT actually return `val'; it returns a new pointer to a copy of the object that val pointed to.
Nice blog, keep discovering and posting!
The last part is incorrect. getInt() (a bad name BTW: newIntFive() or intFiveFactory() would be a better description) creates a new Integer object, a reference to the Integer object and assigns the object reference to val. getInt() then returns a copy of the object reference. There is no implicit object copying here.
Some relevant URLs:
http://oopweb.com/Java/Documents/ThinkingInJava/Volume/TIJ319.htm#Index2146
http://javadude.com/articles/passbyvalue.htm
http://java.sun.com/docs/books/jls/third_edition/html/statements.html#6767
Thank you!
I know that now, but at the time I’d written it, I had been using Java for
less than 6 monthsa very short time. My previous statement was based on a flawed understanding of how Java did garbage collection.Of course, the code example wasn’t meant to do anything, just be an example of allocing an object and returning a pointer.
BTW, if you don’t mind me asking, what lead you here?