0

Displaying array without the Index in java

Tried to display an array without the index, I am getting [I@15db9742. What is the value that is displayed when and array variable ar[] is accidentally programed and displayed System.out.println(ar).; Ideally this should be restricted right?

13th Aug 2017, 3:21 PM
Vivek Zacharia Muricken
Vivek Zacharia Muricken - avatar
1 Odpowiedź
+ 2
The Array class does not override the toString() method that accepts no parameters. So, println() will instead print the Object.toString() method. Object.toString() is something like this: public String toString(){ return className+ '@' +hex(hashCode()); // I just wrote 'className' and 'hex' to shorten this, hopefully you get the idea anyway } So, the class name and hashcode in hexidecimal is printed instead. You can see this by creating & printing your own objects too. class Program{ public static void main(String[] a){ Program obj = new Program(); System.out.println(obj); } }
13th Aug 2017, 3:32 PM
Rrestoring faith
Rrestoring faith - avatar