Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Getting There Type Casting

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,683 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;