Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java pass by value clarified [duplicate]

I have a list of Cell object that represent a board game inside the board class.

Cell boardGame[][] = new Cell[8][8];

I needed a temporary cell to try the player move on him and compare it to the other cells, so I though that I could use a java pass-by-value to do it.

test(board.boardGame);
board.printBoard(); 

private static void test(Cell[][] boardGame) {
Cell c = new Cell((new Soldier(ChessPiece.QUEEN, Color.WHITE)), 7, 4);
    boardGame[7][7] = c;

}

I read some post about java here, but apparently I still didn't catch it 100%.

I expected to see only one white queen on the board, but I saw two. I know that if you pass a reference you can change its values, but I though that if I would pass the array itself its members won't be modified unless I would execute a return.

Please help me to understand this subject better. Thanks

Edit:

I think I don't understand when it called attributes and where it doesn't. I though the different it if you are call "new" or not.

When its part of another object it called attribute right? but every object can be created as part of another object. I can create a new string in dog class and then create the dog class in the animal class and then create it in another class. So only the top class is in the stack?

For exemple:

public class Board  { //fake class

    int num= 0;
    public void test(int i){
        i = 1;
    }
}

and on another class:

    public class Main {
        static void outsideTest(Board board){
            board.num = 1;
        }
    public static void main(String[] args) {
        Board board = new Board();
        System.out.println(board.num);
        board.test(board.num);
        System.out.println(board.num);
        outsideTest(board);
        System.out.println(board.num);
}
}

Now I didn't understand why on test() method the num didn't change and on outsideTest() the num change, num as been created in the heap because its part of the board object, so its need to be changed on both cases no?

like image 837
Smiled_One Avatar asked Feb 09 '26 17:02

Smiled_One


1 Answers

The best and least confusing way to remember it is as follows: Java passes everything by value, that includes the references. :)

When you have a variable:

Object a = new Object();

you don't actually have an object stored in a. What you have is a reference to an object somewhere in memory.

Likewise when you call a method on an object:

String b = a.toString();

you don't do that. What you call is a method that uses the data of the referenced object as its context.

So when you pass an object as an argument

System.out.println(b);

You don't pass the whole object. You pass the reference and that reference is passed by value.

edit:

If the reference were not passed by value, but by reference, you could do something like this, which fortunately you can't.

public void swap(Object a, Object b){
   Object swap = a; a = b ; b = swap;
} 

String a = "a";
String b = "b";
swap(a,b);

// would print "b a" if references were 
// passed by reference but instead prints 
// "a b" as they're passed by value.
System.out.println(a + " " b);
like image 85
M.P. Korstanje Avatar answered Feb 12 '26 15:02

M.P. Korstanje



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!