Porting from C++ code to Jave

Hello There

I am trying to port some c++ code to java but I failed to do it. I have the following definition in C++



[java]#define BOARD_POSITION 320

#define BOARD_WIDTH 10

enum { POS_FREE, POS_FILLED };

int mBoard [BOARD_WIDTH][BOARD_HEIGHT];

mBoard[j] = POS_FREE;[/java]



I tried to port it to Java as following but I failed as the compiler doesn’t accept the type, so I would like to know what am I missing here??



[java]enum POS{ POS_FREE, POS_FILLED }

int [][] mBoard = new int [BOARD_WIDTH][BOARD_HEIGHT];

mBoard[j] = POS.POS_FREE;[/java]

I think you mean POS[][] mBoard.



You should also read up on the standard java coding style conventions as well if coming over from C++. Virtually everyone in java follows the same conventions and it helps readability and consistency a lot.

you can also do

[java]mBoard[j] = POS.POS_FREE.ordinal()[/java] which would be the equivalent in C++. But if your relying on the order and the values have some meaning then instead add a constructor of an int to your enum, and have a get function for it (its what Josh Bloch recommends)



http://www.youtube.com/watch?v=pi_I7oD_uGI#t=10m35s

thank you all



wezrule suggestion is very good.

@kifcaliph said:
thank you all

wezrule suggestion is very good.


Though if you aren't going to use the enum as an enum then you might as well not even define it and just make:
public static final int POS_FREE = 0;
...etc.
@kifcaliph said:
thank you all

wezrule suggestion is very good.


No, it's really very not good as the video he posted explains.

It's the closest to what C would do (which is why he suggested it) but it's terrible Java.

…worse still because it’s halfway between two valid approaches.

@kifcaliph said:
Hello There
I am trying to port some c++ code to java but I failed to do it. I have the following definition in C++

[java]#define BOARD_POSITION 320
#define BOARD_WIDTH 10
enum { POS_FREE, POS_FILLED };
int mBoard [BOARD_WIDTH][BOARD_HEIGHT];
mBoard[j] = POS_FREE;[/java]


Two different Java approaches to the above:
[java]
enum Pos { Free, Filled };
Pos[][] mBoard = new Pos[BOARD_WIDTH][BOARD_HEIGHT];
mBoard[j] = Pos.Free;
[/java]

That's the way the C version would have done it if C had real typing.

[java]
public static final int POS_FREE = 0;
public static final int POS_FILLED = 1;
int[][] mBoard = new int[BOARD_WIDTH][BOARD_HEIGHT];
mBoard[j] = POS_FREE;
[/java]

This is the most accurate representation of what is actually happening in the C version and is the purest translation. It has the added benefit of using half the memory of the first approach... though that probably doesn't matter in this case. And like the C version, the array is automatically initialized as completely POS_FREE.

Really, the right way depends on what else is being done with it.

(Good point on the initialization, the enum version is initialized with null not POS_FREE).



To the OP: Java Enums are extremely powerful, much more so than C ones. It’s well worth reading up on them.