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

Andre Kucharzyk
Andre Kucharzyk
4,479 Points

Why someStuff[0] instanceof Treet = true ?

import com.teamtreehouse.Treet;

import java.util.Date;

Treet treet = new Treet("Sam Harris", "awesome stuff",
        new Date());

Object obj = treet;    <--- treet changed to object!!!

Object[] someStuff = {treet, "a string"};

someStuff[0] instanceof Treet;

Why the outcome of someStuff[0] instanceof Treet = true ??? Wasn't treet changed to Object??

Please somebody give me a hand.

I might be wrong but array starts with 0 and treet is the first one so it is 0. And it is not changed just equals to treet.

1 Answer

Hi Andre Kucharzyk ,

You assigned the treet object to the obj variable. This means that obj now "contains" the treet variable. The treet variable is an instance of the Treet class. So when you call the instanceof method on object, containing the treet object variable which again is of the type Treet, checks if it is indeed an instance of Treet. And since it does, it values to true.