Here it gives me the address lcoation where the String Array is stored. why so ? why does it not give values like List ?
It's up to each object how its toString method works - some are more useful than others. But note that arrays are not "real" objects, they're kind of faked by the compiler and the JVM compared to "proper" classes/interfaces like List. The faking stops short of a useful toString method, though.
here the value at index 1 (ie value 2) gets overridden , why so ? Why doesnt the value at index two move back and five be put at index two and all other indexes shifted backwards?
That's how arrays work - an assignment is not an insertion. Plus, arrays are fixed-length, so extra elements can't easily be added; that's what ArrayList is for.
1. That is not the address but the hasCode value. If you are not overriding the toString() method then the method of object class will be called and whenever you are passing any object to System.out.println() method, system will implicitly call the toString method. In case of Array object this method is not overridden and in case of list it is overridden to return the value in that order.
For the second question related to change in array value please check the API specification here
Joined: Feb 12, 2010
Thanks Harpreet Singh janda for you response.
How can i know which classes override the toString method and which do not ?
Can I say that all collection classes override the toString() method ? Since if the toString() method is not overridden I would get the hashcode value and not the values in that collection. Thanks for responding