Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Understanding List and String.

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should tag your code.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rajeev Trikha
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Above is the default toString() implementation you get from the Object class.
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you got my question wrong. What I meant to ask is this :

All classes have access to toString() since all classes extend java.lang.Object. Can I say that all collection classes have overridden the toString method ?

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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. "

Can i say its only in case of Array object that I would get the hashcode value and all collection classes would provide me their respective values ?
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

All classes have access to toString() since all classes extend java.lang.Object. Can I say that all collection classes have overridden the toString method ?


You can check Java api.

I think yes all collection classes have overriden toString method. AbstractCollection is the class which has overriden toString method. This class is then extended by all collection classes
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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. "

Can i say its only in case of Array object that I would get the hashcode value and all collection classes would provide me their respective values ?

Yes in case of array object you will get the hashcode value.
We dont have any Array class in java.

Also try Arrays.toString() method and then see the result.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic