File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Understanding  List and String. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Understanding  List and String." Watch "Understanding  List and String." New topic
Author

Understanding List and String.

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266

Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
You should tag your code.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41509
    
  53
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.


Ping & DNS - my free Android networking tools app
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

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

Joined: Feb 12, 2010
Posts: 266
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

Joined: Jan 29, 2010
Posts: 85


Above is the default toString() implementation you get from the Object class.

Rajeev Trikha (SCJP 6)
jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
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

Joined: Feb 12, 2010
Posts: 266
"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

Joined: Apr 29, 2008
Posts: 234

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

Joined: Apr 29, 2008
Posts: 234

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Understanding List and String.