Browse in : |
All
> Topics
> Programming
All > Journals > CVu > 124 Any of these categories - All of these categories |
Note: when you create a new publication type, the articles module will automatically use the templates user-display-[publicationtype].xt and user-summary-[publicationtype].xt. If those templates do not exist when you try to preview or display a new article, you'll get this warning :-) Please place your own templates in themes/yourtheme/modules/articles . The templates will get the extension .xt there.
Title: Java Parameter Semantics
Author: Administrator
Date: 03 July 2000 13:15:38 +01:00 or Mon, 03 July 2000 13:15:38 +01:00
Summary:
Body:
Last week John put out a plea for some material on Java, and like the overcommitting fool that I am, I said I would get something to him soon. This short piece is the result of that.
One aspect of Java that frequently trips up the programmer familiar with C++ is pass by reference / pass by value confusion. In a nutshell, Java passes object references by value. So what exactly does that mean? Let us look at an example or two:
public class Voodoo { protected long m_count = 0; public long Count() { return m_count; } public void Add() { ++m_count;} } public class ParameterTest { public static void main( String args[] ) { ParameterTest pTest= new ParameterTest(); Voodoo voo1 = new Voodoo(); System.out.println( "Voodoo count is: " + voo1.Count() ); // 1 pTest.Test( voo1 ); System.out.println( "Voodoo count is: " + voo1.Count() ); // 2 } public void Test( Voodoo voo2 ){voo2.Add();} }
Running the above test shows the output:
Voodoo count is: 0 Voodoo count is: 1
This exceptionally basic test shows us that Java passes by reference (#).
Now consider if Test() had been implemented like this:
public void Test( Voodoo voo2 ) { voo2 = new Voodoo(); voo2.Add(); System.out.println( "Voodoo count is: " + voo2.Count() ); // 3 }
Running this test gives the following output:
Voodoo count is: 0 Voodoo count is: 1 Voodoo count is: 0
In this case, Test() reassigned a new instance of Voodoo to the identifier voo2. It called Count() on this new object, printed out the value of Count() (in this case, the value is 1) then returned.
However, back in main(), when we print out the count at (2) the count is zero again.
Those of you familiar with C++ pass-by-reference may have expected voo1 to have produced the value 2 for the third println, and this is where the difference between C++ and Java becomes apparent. If you remember, at the beginning of this article, I stated:
Java passes object references by value.
In Java, you never get ownership of objects, you merely get ownership of references to objects. In main() the calls to new ParameterTest() and new Voodoo() simply assign references to pTest and voo1 respectively.
The call to Test() which passed in a Voodoo object creates a new reference to the object referenced by voo1. This can be represented by the following ASCII graph:
voo1 → Voodoo object abc ← voo2
This explains the "references by value" part. voo1 and voo2 are two separate references to the same object. In our second implementation of Test() we created a new Voodoo object and assigned a reference to voo2. The graph now looks like this:
voo1 → Voodoo object abc Voodoo object xyz ← voo2
If you think this feature of Java is there just to catch out C++ programmers moving to Java, take heart that an ex-workmate of mine who was a Java programmer writing C++ COM faced the same problems but in reverse when dealing with pointers to pointers ;-)
Notes:
More fields may be available via dynamicdata ..