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

Derek Derek
Derek Derek
8,744 Points

I don't know how to pass this task..

Below is what I have, and I am getting compile time errors, telling me that obj is not a String (error at the line after the if statement). Also, why does it error there if obj is not String since it would not satisfy the if condition and therefore should go to the else..?

public static String getTitleFromObject(Object obj) { // Fix this result variable to be the correct string. if (obj instanceof String) { String result = obj; return result; } else { return (String) obj; }

}

Grigorij Schleifer
Grigorij Schleifer
10,363 Points

I commented TASK 1 a little bit ...

public static String getTitleFromObject(Object obj) { 
// our method accepts Objects as parameter

 String result = "";
// this is your String variable and we want assign the parameter "obj" to it
// if obj is really of String type
// THE PROBLEM IS ---- the compiler only knows that "obj" is an Object
// but has no clue which type this passed Object is
// only we know that obj is a String

   if (obj instanceof String) { 
// so we need to proof whether the passed parameter is of type String
// if obj is in fact a String
result = (String) obj;
   // >>> you can assign the Object "obj" to your String variable "result"
   // >>> to be able to assign an Object into a String variable
   // >>> you need to downcast Object to String type
    }
  return result;  
}
}

2 Answers

Grigorij Schleifer
Grigorij Schleifer
10,363 Points

Hi Hyun,

see here:

https://teamtreehouse.com/community/casting-blogpost

Kevin gives there a great explanation regarding the challenge :)

Happy coding

Grigorij

Hello Hyun,

It looks like you aren't casting result to be a String in your if, and I'm not certain why you've got an else statement there if you're on the first Challenge; if this is meant to be for part 2 of the Challenge as well, you'd want to cast obj to the BlogPost type, then get the title from it. I've included working code below; if you use my code directly for the Challenge, you'd need to remove the extraneous result variable and return statement that are in the Challenge by default.

if (obj instanceof String) {return (String) obj;} else {return ((BlogPost) obj).getTitle();}

Please let me know if you need any clarification on how or why my code works!