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

14,521 Points

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.