The value passed by Java is explicit

See English answer > is java "pass by reference" or "pass by value"? 78

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

I need a temporary unit to try to move it and compare it with other cells, so I can use Java pass by value to complete 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 posts about Java here, but obviously I still haven't caught it 100%

I hope to see only one white queen on the chessboard, but I see two I know that if you pass a reference, you can change its value, but if I pass the array itself, its members will not be modified unless I execute a return

Please help me understand the subject better thank you

Edit:

I don't think I understand when it's called an attribute and what it doesn't If you call it "new", I will be different

When it's part of another object called a property, right? But each object can be created as part of another object I can create a new string in the dog class, then create the dog class in the animal class, and then create it in another class So only the top class is on the stack?

for instance:

public class Board  { //fake class

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

In 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 don't understand why num is not changed on the test () method but changed on the outsidetest (). Because it is part of the board object, Num is created in the heap, so it needs to be changed on both?

Solution

Java is always "passed by value" in essence

The chart below will be clearer

Your 2D array of cell objects should look like anobjectarrayvar (not exactly the ref in the graph pointing to the object. Now it should point to the row. We need to allocate another level in the heap between ref and the objects in each row (a refers to the cell set of the object

Therefore, when you pass boardgame, you pass the value stored in the stack, which stores a reference to the object array (just like the value stored in anobjectarrayvar) If the refs list is stored at position 50, anobjectarrayvar will store the value and pass it to the test method when it is called In this case, the test method cannot go to the memory location anobjectarrayvar and change its value (such as 100), because it only has a copy of the value, but it can easily change the content it references (directly or indirectly) at ref or the next level (and add a new object, such as a new unit with queen in your case) or the object they point to, These changes will reflect the whole program!

I would also like to draw your attention to the facts of the code

boardGame[7][7] = c;

Will replace the current cell (and the soldiers currently in it). If there is a soldier in that place in the game, it will cause major problems The game state will actually change

As a suggestion (considering your limited design knowledge), I would say that at least save the cell in other values in the test method before replacing it

Cell old = boardGame[7][7];
//Now do all your operations
boardGame[7][7] = old;//just before returning from the function
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>