So I made an array:
public static Boolean[][] squaresWithPieces = new Boolean[][]{
{Boolean.TRUE,Boolean.TRUE,null,null,null,null,Boolean.FALSE,Boolean.FALSE},
{Boolean.TRUE,Boolean.TRUE,null,null,null,null,Boolean.FALSE,Boolean.FALSE},
{Boolean.TRUE,Boolean.TRUE,null,null,null,null,Boolean.FALSE,Boolean.FALSE},
{Boolean.TRUE,Boolean.TRUE,null,null,null,null,Boolean.FALSE,Boolean.FALSE},
{Boolean.TRUE,Boolean.TRUE,null,null,null,null,Boolean.FALSE,Boolean.FALSE},
{Boolean.TRUE,Boolean.TRUE,null,null,null,null,Boolean.FALSE,Boolean.FALSE},
{Boolean.TRUE,Boolean.TRUE,null,null,null,null,Boolean.FALSE,Boolean.FALSE},
{Boolean.TRUE,Boolean.TRUE,null,null,null,null,Boolean.FALSE,Boolean.FALSE}
};
and when I run my main() method, which executes the following:
for (int row = 0; row < squaresWithPieces.length; row ) // Cycles through rows.
{
for (int col = 0; col < squaresWithPieces[row].length; col ) // Cycles through columns.
{
System.out.printf("[ ", squaresWithPieces[row][col]); // Change the ] to however much space you want.
}
System.out.println(); // Makes a new row
}
I get the following output:
true true false false false false false false
true true false false false false false false
true true false false false false false false
true true false false false false false false
true true false false false false false false
true true false false false false false false
true true false false false false false false
true true false false false false false false
Why is that the case? I thought using Boolean class allows Booleans to be "null"?
This question also shows that.
CodePudding user response:
Why is that the case? I thought using Boolean class allows Booleans to be "null" ?
Your example highlights the difference between a value and a representation of a value when you print it to the screen. The characters that print out don't necessarily reflect the true value. In this case, you are using the %b
format specifier which can only print true
or false
, even if the actual value is something else.
You can experiment with this by changing %b
to %s
. This might get you a more accurate representation of the underlying values.
CodePudding user response:
Your assumptions are correct but you are printing with the format %b
which always output true or false ("true" if non-null, "false" if null).
CodePudding user response:
It's because you used printf
with a %b
format. Change it to for example System.out.print
and you will see your nulls:
for (int row = 0; row < squaresWithPieces.length; row ) //Cycles through rows
{
for (int col = 0; col < squaresWithPieces[row].length; col ) //Cycles through columns
{
System.out.print(squaresWithPieces[row][col]); // Here!
}
System.out.println(); //Makes a new row
}