0

I've been trying to understand what's happening with the output, when I'm trying to print an empty 2d char array.

import java.util.Arrays;

class Scratch {
    public static void main(String[] args) {
        char[] tictactoe = new char[9];
        tictactoe[0] = '1';
        System.out.println(tictactoe);

        int[][] new2 = new int[2][2];
        new2[0][0] = 2;
        System.out.println(new2[0][0]);

        char[][] ne3 = new char[3][3];
        
        System.out.println(ne3);
        System.out.print(Arrays.deepToString(ne3));
    }
}

This is the output I get:

100000000
2
[[C@9807454   // what does this mean? where does it come from?
[[ ,  ,  ], [ ,  ,  ], [ ,  ,  ]] 

What I don't understand is, what is being printed if I try to output a 2d char array, which cells has not been initialised? I am specifically referring to code System.out.println(ne3); which gives me following output line [[C@9807454 Can anyone kindly explain? Thanks so much in advance!

  • 3
    You're seeing the String returned from a char array's `toString()` method. If you want to print out the array contents, then use `java.util.Arrays.toString(ne3)`. You could also use streams or a for-loop to help you display the array contents. – Hovercraft Full Of Eels Jul 08 '21 at 15:04
  • 1
    I think the OP is trying to print `new2`, not `ne3`, and they should be using `Arrays.deepToString` just like they are for `ne3`. – markspace Jul 08 '21 at 15:16
  • @HovercraftFullOfEels Thanks a lot! I will have to read through toString() method but at least, I know where to start looking now! – jolie-rouge Jul 08 '21 at 15:24

0 Answers0