File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Need more info on toArray(Object[]) 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 "Need more info on toArray(Object[])" Watch "Need more info on toArray(Object[])" New topic

Need more info on toArray(Object[])

Krish Pinnamaneni

Joined: Mar 16, 2005
Posts: 22
Can any body tell me with example regarding following method
I know that List.toArray() returns generalized Object[].
In Advance,
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Just try having this in ur main method:

U will come to know the difference.
In the first method "arList.toArray()", the toArray() method returns Object[]. U cannot convert Object[] to a String[], right? its the same as invoking this:

Which has to throw a run time error.

Now "arList.toArray(new String[3])".
In this method u explicitly specify the run time type of the array so that it returns of the same kind.
Its like invoking

I hope thats clear now
[ March 31, 2005: Message edited by: Animesh Shrivastava ]
Krish Pinnamaneni

Joined: Mar 16, 2005
Posts: 22
Hi Animesh Shrivastava ,

Thanks a lot for spending your most valuable time to give response.

Krish Pinnamaneni

Joined: Mar 16, 2005
Posts: 22
Hi Animesh,
Please correct me if i am wrong.
ArrayList arList = new ArrayList();
String[] str = (String[])arList.toArray();
I know that above code will not be compiled.I need more info. why the code is not being compiled?.

My understandings:

we are adding String's "1" "2" to List.since List.add(Object) those strings are upcasted to generalized Object(i.e. java.lang.Object).so List.toArray() returns Object[].But each element of Object[] can be down casted to String since originally we added String to List.
I don't understand why it is giving compile time error?.Please validate my understandings
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298

String[] str = (String[])arList.toArray();

This shouldnt give compiler error. It shouldnt give any error, thats fine,
But it will give run time (ClassCastException) error, for which i have already given the explanation in my previous post.
But if ur code was

String[] str = arList.toArray();

U would have got a compilation error because its toArray() method returns Object[] so u need to cast it to make String[].
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 812
hey ranchers..
i m still not convienced with animesh...
that why classcastException will be thrown at run time because

till the point we add string to list as object and they are store as bject

so why
String[] str = (String[])arList.toArray();
in this we are also casting...

if anybody pls explain would be gr8 in depth
it gives error and why not
this String[] str1 = (String[])arList.toArray(new String[3]);

Thanks and Regards, Amit Taneja
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206

Object[] objArr = { new String("a"), new String("b") };
String[] strArr = (String[])objArr; //ClassCastException

reason is simple.....objArr is a ref. variable of type Object[] and the object ref. it caontains is also of type Object[], even at runtime it refers to an object which is of type Object[] only(but
objArr instanceof String[] is false, so runtime exception....

if it was :
Object obj = new String("Hello");
String str = (String)obj; //perfectly fine as :
//obj instanceof String is true

hope this helps....

Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97

Line 3 throws ClassCastException but line 4 does not.

Object[] toArray()
returns Object[]


Object[] toArray(Object a[]);
returns String[] as Object[].

SCJP 1.4, SCBCD 1.3
I agree. Here's the link:
subject: Need more info on toArray(Object[])
It's not a secret anymore!