sambandam.gopinath@patni.com Presents your JAVA E-NEWSLETTER for February 5, 2004 <-------------------------------------------> THE GREAT DEBATE: DOES JAVA PASS BY REFERENCE OR BY VALUE? When you ask most programmers if Java passes by reference or by value, you're likely to get one of two answers: (1) Java passes primitives by value; objects by reference; and String by value because strings are immutable. Or, (2) Java passes all arguments by value. Only the second response is correct. The key to understanding the difference is to remember that, when you pass an object to a method, Java doesn't put the object on the stack--it makes a copy of your reference and puts the copy of the reference on the stack. That is, by definition, pass by value. THE PROOF If Java passed objects by reference, then executing the code below would swap the references stored in the two variables, a and b. Here's what the output would be: a: 4 b: 100 Swapped! a: 100 b: 4 What it really does is swap the references in the method arguments o1 and o2, which doesn't affect the original variables, a and b. Here's the actual output: a: 4 b: 100 Swapped! a: 4 b: 100 public class SwapTip { public static void main(String []args) { Integer a = new Integer(4); Integer b = new Integer(100); System.out.println("a: " + a); System.out.println("b: " + b); swap(a, b); System.out.println("Swapped!"); System.out.println("a: " + a); System.out.println("b: " + b); } public static void swap(Object o1, Object o2) { Object t = o1; o1 = o2; o2 = t; } } WHY DOES THE ANSWER TO THIS DEBATE MATTER? Many programmers who have debated the pass-by question end up saying, "It's only a difference of semantics," or "it doesn't matter because everyone understands how it really works." It may just be a matter of semantics to the experienced programmer but, to the inexperienced programmer, it does make a difference. The less of a black box that a language is to programmers, the better programmers will be when they create programs in that language. AN ANALOGY Object references are to instances as remote controls are to televisions. A reference controls an object like a remote control controls a television. If a copy of the remote control is given to a second person, that person can also control the television. All changes made to the television by the duplicate remote control (e.g., volume, channel, clock) are seen by all. If that person reprograms their copied remote to control another television, the original remote control is unaffected. Whether Java passes by value or by reference can admittedly be an academic distinction--as long as you know what behavior to expect. At times, it's important to know what's happening behind the scenes. Java passes only by value. It's simple, documented, and true. So, the next time you hear the question, you'll know the right answer. David Petersheim is the Director of Application Development with Genscape, Inc. He designs and develops server-side applications to acquire and process real-time energy data. ---------------------------------------- String Message = "User does not have an Edit lock on the package. Save aborted."