Arch1tect
Arch1tect

Reputation: 4281

Java reference and new keyword

I'm writing a simple chessboard while learning Java and come up with the following question:

I have abstract class ChessPiece and classes like King, Queen, Rook etc. which all extend ChessPiece.

And I have a 2D array of ChessPiece for the chessboard.

public ChessPiece myChessBoard[][] = new ChessPiece[8][8];

Then I make my chess pieces like this:

ChessPiece aPiece = new Rook();

And put it on myChessBoard so I can find it:

myChessBoard[0][0] = aPiece;

Same goes for other pieces...

My question is when I do this public ChessPiece myChessBoard[][] = new ChessPiece[8][8]; in my first step, I used new keyword and allocated 8*8 = 64 ChessPiece objects right? But I did it again when writing ChessPiece aPiece = new Rook(); I feel stuff in myChessBoard such as myChessBoard[0][0] don't need to be allocated since it's just a reference and don't need that much space as a real chess piece like new Rook().

In short I feel I only need to allocate 32 chess pieces with new keyword (32 pieces on a chessboard) instead of 32 + 64 = 96 chess pieces.

I might have confused some basic concept in Java, please help.

Edit:

myChessBoard[][] = new ChessPiece[8][8] here I used new keyword to create 64 reference without real object. So new keyword doesn't necessarily create new objects, it can also create new references referring to null, right?

that is wrong. "the new keyword in that case is creating a new 2D array Object" thanks to accepted answer.

Upvotes: 3

Views: 290

Answers (6)

Paul Samsotha
Paul Samsotha

Reputation: 209072

All you're doing with this public ChessPiece myChessBoard[][] = new ChessPiece[8][8]; is initializing a new game board. The game board has 64 squares (each square represented by a 2D index value), not 64 chess pieces. So each square can be given a chess piece value like myChessBoard[0][0] = new Rook();

// or to give your pieces references
Knight blackKnight1 = new Knight();
myChessBoard[0][1] = blackKnight1;

Bishop blackBishop1 = new Bishop();
myChessBoard[0][2] = blackBishop1;

Queen blackQueen = new Queen();
myChessBoard[0][3] = blackQueen;

// and do the rest with all the other pieces according 
// to their position in the 2d array

Upvotes: 4

noone
noone

Reputation: 19776

When doing public ChessPiece myChessBoard[][] = new ChessPiece[8][8]; you did not create a single ChessPiece. You only created an Array which is capable of having a reference to 64 ChessPieces.

All references will initially be null and you have to set them like myChessBoard[1][4] = new Rook()

So there is no need to worry.

I assume your ChessPiece class is abstract anyway, so it is impossible for Java to create a ChessPiece anyway.

But one more point: Java has automatic garbage collection. So even in case you do create some unnecessary objects sometimes, the garbage collector will get rid of it pretty quickly. Don't optimize prematurely. Every computer nowadays can handle 96 or 32 objects and there won't be any noticable difference.

Upvotes: 3

iluxa
iluxa

Reputation: 6969

When you create the arrays, you simply allocate the storage cells that start of as nulls, but can also be used to store chess pieces later, and you create 64 of these. No chess pieces so far. Those still need to get created separately.

Suppose you've built a prison with 10,000 cells. That's great in itself, but you still have to find 10,000 prisoners to populate it.

Upvotes: 1

musical_coder
musical_coder

Reputation: 3896

When you call ChessPiece myChessBoard[][] = new ChessPiece[8][8];, you're not instantiating any ChessPiece objects. You're just creating a 2D array that can hold them. To give a real-life analogy, when you buy plastic containers to hold leftover food, they don't come with food in them- you put that into the containers later.

Upvotes: 1

Mureinik
Mureinik

Reputation: 311998

public ChessPiece myChessBoard[][] = new ChessPiece[8][8] is an array allocation statement. It does not allocate 64 new ChessPiece objects, but a two dimensional array containing 8*8=64 references to ChessPieces, most of which are null. Only wen populating each individual square do you actually create a new ChessPiece object.

Having said that, you do have a point, and this allocation policy does seem somewhat wasteful. My suggestion to you, since this is a learning project - hide the chessboard implementation in its own class (e.g., ChessBoard), which for now will contain your two dimensional array. Once you've got everything else working to your satisfaction, you can go back and optimize the memory consumption of ChessBoard.

Upvotes: 1

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79876

No. Writing new ChessPiece[8][8] does NOT allocate 64 ChessPiece objects. It only creates an array, with 64 null references in it. You need to make the ChessPiece objects separately, which is what you're doing.

Note that many OOP languages (in particular, C++) work differently in this respect.

Upvotes: 1

Related Questions