Results 1 to 6 of 6

Thread: Passing by reference in Java

  1. #1
    Join Date
    Mar 2010
    Posts
    162

    Passing by reference in Java

    Hello,
    For basic types as int, char. Passing by value. For objects, passing by value of the reference (by abuse of language, passed by reference).
    Here is the code
    Code:
    str = mstr "Hello";
    exchange(mstr);
    System.out.println(mstr);
     
    void exchange(str unstr)
    {
       unstr = Goodbye;
    }
    So, if any one can explain me then it would be great. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    347

    Re: Passing by reference in Java

    Hello,
    See, this code
    Code:
    void exchange(String unstr)
    Has the appeal of the method changes (mstr), the reference of the object is referenced by mstr fill in the local variable to the method that is the parameter unstr
    See here
    HTML Code:
    unstr = Goodbye;
    You replaced the reference in unstr by reference to the string constant "goodbye". The variable mstr is in no way affects the reference and storage on the same subject before.

  3. #3
    Join Date
    Nov 2009
    Posts
    343

    Re: Passing by reference in Java

    Hello,
    In your method changes when you do
    Code:
    unstr = "goodbye"
    you change just the value of the parameter "unstr", which is a copy of mstr. So obviously, you do not change mstr
    Code:
    void exchange(unclassmutbl subject) {
        object.setQuelqueChose(newValeur());
    }
    Here for cons, you change the contents of the object passed as parameter (but there is no setter on the String class (because String is immutable), you can not change its value).

  4. #4
    Join Date
    Nov 2009
    Posts
    518

    Re: Passing by reference in Java

    Hello,
    Passing by value means "passing by copying the value." So when you pass an object you pass a copy of the reference, and if that reference is changed that does not impact the reference of the calling code (the new reference exists in the method body).
    By cons, if the object is changed, so there it works:
    Code:
    void exchange(StringBuffer strbuf)
    	{
    		strbuf.delete(0Sb.length()); / / We erase all
    		strbuf.append(Goodbye);
    	}
    And see this code
    Code:
    		StringBuffer mstr = New StringBuffer("Hello");
    exchange(mstr);
    System.out.println(mstr);

  5. #5
    Join Date
    Nov 2009
    Posts
    335

    Re: Passing by reference in Java

    Hello,
    Otherwise if you have not been changed, you can always do something like that
    Code:
    str = mstr "Hello";
    Object[] t = New Object[] {mstr};
    exchange(t);
    mstr = t[0];
    System.out.println(mstr);
     
    void exchange(Object[] t)
    {
       t[0] = Goodbye;
    }
    This is somewhat hidden misery but sometimes there is no alternative

  6. #6
    Join Date
    Nov 2009
    Posts
    583

    Re: Passing by reference in Java

    Hello,
    Or they choose a gender approach to CORBA:
    Code:
    class GenericHolder <T>
    {
      public T val;
    
      public GenericHolder(T aval)
      {
        val = aval;
      }
    }
    But in general it is preferable to avoid this kind of transition parameters. It's not for nothing that AC was not included in the Java language.

Similar Threads

  1. Passing optional parameters in java
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 05-03-2010, 10:20 AM
  2. Message Passing in Java
    By rashmi_ay in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 10:33 PM
  3. Java & Oracle: Passing Date
    By garfield1 in forum Software Development
    Replies: 5
    Last Post: 04-01-2010, 12:30 PM
  4. Best reference book for java
    By Sheenas in forum Software Development
    Replies: 5
    Last Post: 19-12-2009, 08:15 AM
  5. Problem Passing Data Dynamically in Java
    By Kushan in forum Software Development
    Replies: 3
    Last Post: 26-03-2009, 02:26 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,028,896.37198 seconds with 16 queries