Hi,

I am new to Java, but have been programming in C++ for a few years now. I am working on a project where I have to write a class to implement a gameboard. To do so, I'm using a 2D array of objects. I have a method that swaps two lines in the 12 X 12 matrix. What I have done is set up a new 1D array of the object type and temporarily stored one of the rows/columns to be swapped into this array, I then copy it's contents into the other row/column. My question is, when I am done with this tempArray of objects, must I deallocate it somehow, or does garbage collector take care of this??

Here is my code for this method:

public void swapLines(char xORy, int line1, int line2){

        Tile [] tempLine = new Tile [12];

        if(xORy == 'X'){

            for(int i = 0; i <= 11; i++){

                tempLine[i] = new Tile(board[i][line1].getLetter(), board[i][line1].getValue());

            }

           for(int i = 0; i <= 11; i++){

               board[i][line1] = board[i][line2];

           }

           for(int i = 0; i <= 11; i++){

               board[i][line2] = tempLine[i];

           }

        }

        if(xORy == 'Y'){

            for(int i = 0; i <= 11; i++){

                tempLine[i] = new Tile(board[line1][i].getLetter(), board[line1][i].getValue());

            }

            for(int i = 0; i <= 11; i++){

               board[line1][i] = board[line2][i];

           }

           for(int i = 0; i <= 11; i++){

               board[line2][i] = tempLine[i];

           }
        }
    }

Any pointers would be greatly appreciated (no pun intended).

Thanks,

Gadgetman_53

Dani AI

Generated

Short answer: no explicit deallocation is required — the JVM’s garbage collector reclaims objects that are no longer reachable. In the posted method the temporary array itself (the Tile[] created with new) becomes eligible for collection once the method returns and there are no remaining references to it. Individual Tile objects become collectible only when no live references point to them. Note that in the original code the Tile instances placed into tempLine are later stored back into board (board[...]=tempLine[...]), so those particular Tile objects remain reachable and will not be collected after the method ends.

A couple of clarifying points to complement ’s reply: explicitly assigning local variables to null is rarely necessary — local references naturally go out of scope when the method returns. Nulling can make sense inside a long-running method if holding a large object reference would otherwise delay reclamation, but for short methods (like a 12×12 swap) it’s unnecessary. Calling System.gc() is not recommended as it only suggests GC and harms predictability.

If the goal is merely to swap entries without allocating new Tile objects, swapping references in-place avoids allocations entirely. For columns (swap per row):

for (int r = 0; r < board.length; r++) {
    Tile tmp = board[r][col1];
    board[r][col1] = board[r][col2];
    board[r][col2] = tmp;
}

For swapping whole rows (constant time):

Tile[] tmp = board[row1];
board[row1] = board[row2];
board[row2] = tmp;

These approaches do shallow swaps; if Tile is mutable and each slot must have an independent copy, then a copy/clone method is appropriate and allocations are unavoidable.

Final tips: use board.length and board[0].length instead of hard-coded limits, validate indices and return early when line1 == line2, and consider reusing a single temp buffer or synchronizing access for performance or concurrency needs. This keeps behavior predictable and avoids unnecessary allocations on larger boards.

Recommended Answers

All 2 Replies

Garbage collector does this for you periodically or when it detects that they are no longer being used. Usually the reference to a variable is lost when the variable goes out of scope.

If you want to do it explicitly set the value of the variable to null(I guess).

Thanks for the answer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.