Val's Blog
Lots of stuff for Web 2.0 freaks and Java addicts
Feeds RSS | Atom | RDF
 
 
C.A.R. Hoare: "The unavoidable price of reliability is simplicity."
[ Login ]

May 2004
SunMonTueWedThuFriSat
       1 
 2  3  4  5  6  7  8 
 9  10  11  12  13  14  15 
 16  17  18  19  20  21  22 
 23  24  25  26  27  28  29 
 30  31      
Apr  |  Today  |  Jun
XML Feeds   Subscribe with Bloglines

Javaranch Sheriff   My LinkedIn Profile
Drop me a line or two   Bloglines Blogroll
JavaRSS   Referers
How cool are you?   My Reviews

Next trips...
SpringOne 2008 (Jun 11-12, 08)
Ajax Exp. 2008 (Sep 29-Oct 1, 08)
Top 10 entries (#hits)
(As of Nov 30, 2007)


Top 10 entries (#hits/day)
Come Back (5.032)
(As of Nov 30, 2007)
Recent Blog Entries
Recent Blog Comments
Re: Review of "Marketing Management 12th"
i know marketing management by kotler is good book but the problem is that the management part of this book is totally missing as fare as i know managemet is complete different subject and it should not be mixed i am student of MBA i was looking at ass...

Re: Review of "Pro Spring"
Using simple POJOs + factories without Spring for "echo" and "counter" would be a lot more easier. No need to write those XML files... So, in this case using Spring makes me write a lot more code... (OK, you can generate everything with the help of And...

pls urgent
Hi I am trying to generate the word doc but i m not understanding wats happening any one pls figure it out /* * WordAPI.java * * Created on May 30, 2006, 10:50 AM * * To change this template, choose Tools | Template Manager * and open the te...
Archives (# entries)
Links
Other Blogs
Other Blogs

Reviewing
Reading
Locations of visitors to this page
What they once said...
 

It's no secret that primitive and reference arguments are always passed by value in Java. This means that a copy of the primitive value, respectively a copy of the reference to an object, is actually passed on to the method as depicted in the small example below.

Code view Object graph view
    [...]
5.  public void method1() {
6.    Vector v1 = new Vector();
7.    method2(v1);
8.  }
9.  public void method2(Vector v2) {
10.   // the original reference to the Vector
11.   // object in method1 does not change
12.   v2 = new Vector();   
13. }
    [...]
  
  Line 6: v1 -----> Vector 1

  Line 7: v1 -----> Vector 1
                       ^
                       |
          tmpcopy -----+
  // tmpcopy is a copy of the reference 
  // v1 that is passed to the method

  Line 9: v1 -----> Vector 1
                       ^
                       |
          v2 ----------+
  // v2 is conceptually the same
  // reference as tmpcopy

  Line 12: v1 -----> Vector 1

          v2 ---> Vector 2
  

In clear, every primitive or reference value that gets passed to a method usually goes unaffected and the caller can be sure that the value he passed has not been changed in the body of the called method.

The designers of the Java language originally decided not to include pass-by-reference semantics into the language in order to prevent some obscure things that have been known to happen in C++ programs to occur within your Java program. Briefly, in Java all parameters are IN parameters, which doesn't mean that you cannot invoke methods on the reference you get in order to alter the state of the object!!

Now, there are some areas like distributed computing where it might be sometimes desirable to pass objects around by reference. For instance, CORBA has the concept of "Holder" classes to simulate the behavior of INOUT or OUT parameters. Holder classes usually contain a reference to some value that the caller wants to be changeable from within the called method. By using OUT or INOUT parameters, a method may possibly "return" several values. This wrapping trick can help you bypass Java's requirement of having at most one value being returned from a method call. A small academic example illustrates this concept below:

    // In file Holder.java
1.  public class Holder {
2.    public String value;
3.    public Holder(String initial) {
4.      this.value = initial;
5.    }
6.  }
    
    // In file Test.java
1.  public class Test {
2.    public static void main(String[] args) {
3.      String hello = "Hello";
4.      Holder holder = new Holder(hello);
5.      method1(hello);
6.      String ret = method2(holder);
7.      System.out.println(hello); // prints "Hello"
8.      System.out.println(holder.value + ret); // prints "Hi James"
9.    }
10.   public static void method1(String s) {
11.     s = "Hi";
12.   }
13.   public static String method2(Holder h) {
14.     h.value = "Hi ";
15.     return "James";
16.   }
17. }
  

You could as well use an Object[] array and modify its components from within the called method. Moreover, since primitives are not objects (at least until the Tiger gets unleashed), they are always passed by value. If you want the called method to change the value of primitives you pass to it, you just have to wrap them within a primitive array (i.e., int[], long[], etc) and modify the values directly in the called method.

On the other hand, you might ask yourself whether you really need to return several values from your method calls. Usually, when this case arises, it is usually a sign of poor object-oriented design. Using OUT or INOUT parameters makes your code harder to understand and to maintain. The semantics of the Java language is usually sufficient for solving 99.9% of the problems you may come up with. So maybe, you might have to rethink your design and see if the values you have to return might not be better bundled in some new class...

Personally, I must admit that I have never really used this technique in any of my applications and I don't think it brings much business value, but it is good to know that it is possible to simulate pass-by-reference semantics and to be able to "return" more than one value from a method call. Not all inventions and findings are useful ;)




Add a comment

Title
Body
HTML : b, i, blockquote, br, p, pre, a href="", ul, ol, li
Math Quiz 7 + 5 = (Helps stop blog spam)
Name
E-mail address
Website
Remember me Yes  No 

E-mail addresses are not publicly displayed, so please only leave your e-mail address if you would like to be notified when new comments are added to this blog entry (you can opt-out later).

TrackBack to http://radio.javaranch.com/val/addTrackBack.action?entry=1085125887000

 
About this Blog