1

I am trying to print the elements of this array, but it is printing junk characters. What is the problem in this program?

class Demo{     
    public static void main(String[] args){
        int[] x= new int []{5,6,7,8,9,10,11};
        {
            System.out.println(x);
        }
    }
}
b4hand
  • 9,550
  • 4
  • 44
  • 49
jtu
  • 29
  • 4

2 Answers2

1

You need to use the Arrays.toString() method:

System.out.println(Arrays.toString(x));

This gives you a meaningful representation of the data inside your array. The default toString of Java array objects is not a meaningful representation of the data.

Kon
  • 10,702
  • 6
  • 41
  • 58
0
public static void main(String args[]) {
         int[] x= new int[]{5,6,7,8,9,10,11};
         {
             System.out.println(Arrays.toString(x));
         }
    }
Ritesh Karwa
  • 2,196
  • 1
  • 13
  • 17