Passing object to method in java appears to be by reference (and Java is by val)

I thought when you passed objects to methods in Java, they were supposed to be by value.

Here is my code:

public class MyClass{
    int mRows;
    int mCols;
    Tile mTiles[][];     //Custom class

    //Constructor
    public MyClass(Tile[][] tiles, int rows, int cols) {
        mRows = rows;
        mCols = cols;

        mTiles = new Tile[mRows][mCols];
        for (int i=0; i < mRows; i++) {
            for (int j=0; j < mCols; j++) {
                mTiles[i][j] = new Tile();
                mTiles[i][j] = tiles[i][j];
            }
        }
    }

At this point, any changes to the mTiles object are reflected back to the tiles object. Any ideas how to fix this?

Thanks all.


It is by val, but when talking about objects, what gets passed is the value of the reference, so, if your object is mutable ( as in your example ) modifying the object through the reference copy, will modify the underlaying object.

To solve it, you have to copy your object and pass that new reference value.

In your code, you are using the same reference value in "tiles" and in "mTiles"

mTiles[i][j] = new Tile(); // <---this line is useless by the way
mTiles[i][j] = tiles[i][j] // because you then assign the same value here

You would have to create a new one like this:

mTiles[i][j] = new Tile(tiles[i][j]);

Or

mTiles[i][j] = tiles[i][j].clone();

Or

mTiles[i][j] = Tile.newFrom( tiles[i][j] );

Or something like that, so you can create actually a new one, instead of using the same ref.

I hope this helps.

EDIT

When you change the ref in pass-by-val, the original is not affected, ie:

String s = "hi"; 
changeIt(s);
....
void changeIt(String s){ 
    s = "bye" // assigning to the copy a new ref value of "bye"
}

After this, the original "hi" is still "hi". In pass-by-ref it would be "bye"

Here some links:

http://www.javaranch.com/campfire/StoryPassBy.jsp

Can someone explain to me what the reasoning behind passing by "value" and not by "reference" in Java is?

Pass by reference or pass by value?

http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

http://academic.regis.edu/dbahr/GeneralPages/IntroToProgramming/JavaPassByValue.htm

http://www.jguru.com/faq/view.jsp?EID=430996

An ultimately:

http://www.google.com.mx/search?sourceid=chrome&ie=UTF-8&q=java+is+pass+by+value


你不传递对象,你传递一个对象的引用,这是通过值复制的。


Here are some diagnostic examples for Java's argument passing semantics:

For an object type:

void changeIt(String s) {
    s = "bye";
}

String s = "hi"; 
changeIt(s);
System.out.println(s);  // would print "bye" for call by reference
                        // but actually prints "hi"

For a primitive type:

void changeIt(int i) {
    i = 42;
}

int i = 0; 
changeIt(i);
System.out.println(i);  // would print "42" for call by reference
                        // but actually prints "0"

In fact, in both of those examples, the assignments within the changeIt methods only affect the respective method's local variable, and the actual values printed will be "hi" and "0".

EDIT

And since the OP still doesn't believe me ... here's a diagnostic example to show that Java is call-by-value for mutable objects as well.

public class Mutable {
    int field;
    public Mutable(int field) { this.field = field; }
    public void setField(int field) { this.field = field; }
    public int getField() { return field; }
}

void changeIt(Mutable m, Mutable m2) {
    m = m2;  // or 'm = new Mutable(42);' or 'm = null;'
}

Mutable m = new Mutable(0); 
Mutable m2 = new Mutable(42); 
changeIt(m, m2);
System.out.println(m.getField());  
                        // would print "42" for call by reference
                        // but actually prints "0"

By contrast, this example will give the same answer for both call by reference and call by value semantics. It doesn't prove anything about argument passing semantics.

void changeIt2(Mutable m) {
    m.setField(42);
}

Mutable m = new Mutable(); 
changeIt2(m);
System.out.println(m.getField());  
                        // prints "42" for both call-by reference
                        // and call-by-value

Trust me, I've been programming Java for > 10 years, and I've taught comparative programming language courses at university level.

链接地址: http://www.djcxy.com/p/20664.html

上一篇: BitmapData.rect创建一个新的矩形?

下一篇: 在java中将对象传递给方法似乎是通过引用(而Java是通过val)