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?
1 Answer
+ 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);
}
}