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

omid nassir
omid nassir
8,771 Points

I don't understand this task

Hi everyone,

I don't understand the concept for this task. I understand that the getTitleFromObject() will be passed a String and/or a BlogPost, so it will be of type Object.

But when the step says "return the obj type casted as a String if it is in fact a String", does that mean I need to check if a String has been passed in? And if so, why does the name of the method start with "getTitle"? The steps don't mention anything about a title.

So far for my code I have written:

public Object getTitleFromObject(Object obj) { if (obj instanceof String) { return obj; } }

Additionally, how can I format my question so that my code segment appears in the the respected box? Thanks.

1 Answer

Nicholas Pretorius
Nicholas Pretorius
18,671 Points

Hi Omid,

You are on the right track!

If the obj is an instanceof String, then cast the obj to the type of String and assign it to result.

In order to get your code in the block, copy your lines of code, then wrap it within three backticks (opening and closing backticks). Click the Markdown Cheatsheet below the window to see an example.

if (obj instanceof String) {
  result = (String) obj;
return result;