Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Data Structures Getting There Type Casting

Christian Thieme
Christian Thieme
9,448 Points

Why is he adding the (Treet) before the code in this - ((Treet) someStuff[0]).getDescription();

I don't understand why he is adding the (Treet) before the code in this - ((Treet) someStuff[0]).getDescription();

I get all the code after the Treet, but don't understand why he added that at the beginning.

This is not the best treehouse video i've ever seen. This one could be revamped.

1 Answer

Seth Kroger
Seth Kroger
56,403 Points

In this video, someStuff is an array of Objects. Because each item in the array is treated like an Object, and Objects don't have a getDescription method like Treet does, the item needs to be cast to a Treet to access it. It's equivalent to:

Treet aTreet = (Treet) someStuff[0];
aTreet.getDescrption();

It's just compressed into one line, and the extra parentheses are there to make the order of operations correct (to cast the item from the array, not the result of getDescription()).