2

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("%5b ", squaresWithPieces[row][col]);  // Change the %5d 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.

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
DerPascal
  • 43
  • 3
  • 2
    Rather than asking "why does Java", it's useful to ask *which* part of "Java" is doing this to you. It's not that you said "put a null in the array", but it puts a false value there instead; you can easily determine that (e.g., look at it in the debugger.) This sort of divide-and-conquer will quickly lead you to "printf" being the actor here. Once you have that, its pretty easy to see why; it would rather produce some output (since printf is usually for debugging output) than throw an NPE and leave you wondering "where did the null come from." – Brian Goetz Mar 14 '22 at 22:23
  • From the Javadoc: _"'b', 'B' - If the argument arg is null, then the result is "false". If arg is a boolean or Boolean, then the result is the string returned by String.valueOf(arg). Otherwise, the result is "true". "_ – Jim Garrison Mar 14 '22 at 23:18

3 Answers3

4

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
2

It's because you used printf with %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
}
 
radrow
  • 6,419
  • 4
  • 26
  • 53
1

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).

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50