1

Possible Duplicate:
Is Java “pass-by-reference”?

I want to know how to pass an object as a parameter to a method by referance in java. I tried this code

public class Class1 
{
    public Class1()
    {
        String x = null;
        F(x);
        //x = null
        System.out.println("x = " + x);
    }

    void F(String x)
    {
        x = "new String";
    }

    public static void main(String[] args)
    {
        new Class1();
    }
}

You can see that I pass a String to a function F and I change String's value inside it, but I can't see the changes I made on it outside the Function F. When I execute this code, I got //{x = null} which is not what I expected //{x = new String}.

Community
  • 1
  • 1
Wazani
  • 911
  • 1
  • 13
  • 28

5 Answers5

10

The objects themselves are passed by reference, but that reference is passed by value.

What this means is that you can change the objects in your function, i.e. you can change their fields, call methods on the object that change it's state, etc.

You're trying to change the reference in your function. That reference is only a copy.

Also note that String is an immutable type. So even with it's reference, you can't change the underlying Object.

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • 1
    Yes, and in this case Strings are secretly immutable in Java. So you're actually getting back a reference to a new String object from the pool of Strings. – Matt Whipple Sep 14 '12 at 17:45
  • 2
    @tuğrulbüyükışık there is no `set` method on `String` – pb2q Sep 14 '12 at 17:50
  • @pb2q maybe .concat("sdfdsfsd") works – huseyin tugrul buyukisik Sep 14 '12 at 17:54
  • 1
    RE first sentence: The objects are *not passed at all*, if we're going to distinguish between objects and references (which we must to explain the difference). Most importantly, there is nothing remotely like pass-by-reference, because despite also using the term "reference", it does not refer to passing something called a reference, but to passing a reference to a *storage location* (variable, array element, object field, etc).. –  Sep 14 '12 at 17:54
2

Java passes arguments by value not by reference. See my answer on another question for an explanation by figures.

However, you can achieve what you are trying to do by:

1- Using arrays:

public class Class1 
{
    public Class1()
    {
        String x = null;
        String[] holder = {x};
        F(holder);
        //x = null
        System.out.println("x = " + holder[0]);
    }

    void F(String[] holder)
    {
        holder[0] = "new String";
    }

    public static void main(String[] args)
    {
        new Class1();
    }
}

2- Using wrapper class:

class WrapperClass
{
    public String value;
}
public class Class1 
{
    public Class1()
    {
        String x = null;
        WrapperClass w = new WrapperClass();
        w.value = x;
        F(w);
        //x = null
        System.out.println("x = " + w.value);
    }

    void F(WrapperClass wrapper)
    {
        wrapper.value = "new String";
    }

    public static void main(String[] args)
    {
        new Class1();
    }
}
Community
  • 1
  • 1
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
2

Java objects are passed by reference. Means when you create a object and assign it to a reference(variable) its address is assigned to it.. and when you modify this in the called function it modifies the same object passed. But in your case u have passed null which is not associated to any object.

See my Example to get clear idea.

public class Class1 {
public Class1() {
    StringBuffer x = new StringBuffer("Before");
    F(x);
    // x = null
    System.out.println("x = " + x);
}

void F(StringBuffer x) {
    x.append("After");
}

public static void main(String[] args) {
    new Class1();
}

}

1

Since the beginning of Java, Strings were a 'special' kind of object. They do live on the Heap but to save memory and improve performance the JVM will keep a count of identical strings and point all references to the location with the same value. But for this to work the String in memory has to be immutable (frozen, they cannot be changed anymore).

An alternative would be to modify your code so that your function returns the updated value:

public class Class1 
{
    public Class1()
    {
        String x = null;
        x = F(x);
        //x = null
        System.out.println("x = " + x);
    }

    String F(String x)
    {
        return x = "new String";
    }

    public static void main(String[] args)
    {
        new Class1();
    }
}

But maybe you were passing an argument for a reason and this is just to illustrate how your method can return a new String.

Salvador Valencia
  • 1,330
  • 1
  • 17
  • 26
0

you cant to that because sting are Immutable that is not changeable.you can use StringBuffer it is mmutable.ok

class One
{
    One()
    {
        StringBuffer x =new StringBuffer("null");
        F(x);
        //x = null
        System.out.println("x = " + x);
    }

    void F(StringBuffer x)
    {
        x = x.delete(0,4);
        x=x.append("new string");   
    }

    public static void main(String[] args)
    {
        new One();
    }
}
Neeraj singh
  • 257
  • 1
  • 8
  • 18