Are cloned java char[][] objects totally new?

For a chess whip up I'm doing,
I'm using a char 8x8 to store the board state. White uppercase, black lower.
I'm noticing that passing various board objects around gets moves recurring on each other, pointer madness.
If I pass .clones of the board object into methods, will it be a true copy of the character[][] grid, - new [][] and primitives created?


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

No clone() won't copy the array - you'll get 2 references to the same array object, not what you want.

To see this try this block of code.  First run it with custom "clone()" method commented out, then run it with that method present.  You'll see the difference in the output for whether it truly cloned or not.  (i.e. You have to write your own clone code to make this work - Java won't do it for you).


import java.util.Arrays;

public class Board implements Cloneable {
	public int[][] m_pieces = new int[8][8] ;

	public Board copy() throws CloneNotSupportedException {
		Object copy = this.clone() ;
		return (Board)copy ;

	protected Object clone() throws CloneNotSupportedException {
		Board copy = new Board() ;
		for (int i = 0 ; i < m_pieces.length ; i++) {
			copy.m_pieces[i] = Arrays.copyOf(m_pieces[i], m_pieces[i].length);
		return copy ;

	public static void main(String[] args) throws CloneNotSupportedException {
		Board first = new Board() ;
		first.m_pieces[0][0] = 1 ;
		Board second = first.copy() ;
		System.out.println("First has " + first.m_pieces[0][0] + " second has " + second.m_pieces[0][0]);
		second.m_pieces[0][0] = 2 ;
		System.out.println("First has " + first.m_pieces[0][0] + " second has " + second.m_pieces[0][0]);

Open in new window

beavoidAuthor Commented:
That's what I suspected :)

I'm going with this...?

char[][] superClone(char[][] board) {
		char[][] returnBoard = new char[8][8];
		for (int y=0; y<8;y++)
			for (int x=0;x<8;x++)
				returnBoard[x][y] = board[x][y];
		return returnBoard;

Open in new window


Yes that's fine.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.