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