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 trialjoseph sack
3,539 PointsI don't feel that type casting was explained in enough detail in the previous video in order to solve this exercise.
yup
2 Answers
Grigorij Schleifer
10,365 PointsHi joseph,
let me comment the code a little bit:
public static String getTitleFromObject(Object obj) {
// getTitleFromObject takes an Object as argument
// in Java everething is an object except primitives like "int", "double" etc...
// so you can give a Treet or BlogPost as argument because they are objects
// the problem is that the compiler doesnยดt know what kind of object you gave as argument (just you know it!!!)
// Treet is an object so the compiler accepts it, but has no clue which object it is
// so you have to tell the comiler which kind of object you gave as argument
// so you need to cast/convert/specify it (telling which kind the object is)
String result = "";
if(obj instanceof String) {
// the line above means
// if the argument obj (Object) is an instance of the String class
// then it should be an object of String class
// do the line below
result = (String)obj;
// tells the compiler that our argument is an object of kind of String and not only an undefined object
}
return result;
}
Does it make sense?
Grigorij
Lewis Cowles
74,902 PointsI don't feel like you watched the video before the task
public static String getTitleFromObject(Object obj) {
// Fix this result variable to be the correct string.
String result = ""; // Default value
if( obj instanceof String ) { // Craig talked about this instanceof after casting
result = ((String)obj); // uses the casting shown in the video
}
return result;
}
joseph sack
3,539 PointsI don't quite see how I could complain about a class I didn't watch.. logic is critical in this field.. I'm just saying man
P.S. I've watched it quite a few times and even transcribed it at 25% which was pretty helpful, check it out if the lecturer ever talks to fast or to technical.
Lewis Cowles
74,902 PointsThe video is simple Joseph, I'm sorry but if you didn't get it, but it's not on Craig Dennis, the code I have shown is 1:1 the syntax used in the video for instanceof
and the type-cast (Although I would personally prefer a ternary).
As for example 2, it's just an else if statement if( obj instanceof SomeOtherClass)
, cast to the required class and store in variable; then get the title as the result... The second part of the challenge may have required slightly more thought, but I felt the video covered it in more than enough detail.
As for some of the other comments in this thread. C has type casting as well, you mention you know C. Instead of creating a pointer to an address space of an existing object, this language uses parenthesis to perform this itself in a more "managed" (type compatible) way.
joseph sack
3,539 Pointsjoseph sack
3,539 PointsYeah, i like your explanation at the top. On all Forum threads I checked, only step 1 is solved, that was pretty easy, but step 2 was so hard and it was certainly not explained in a way a java beginner could understand. I'll add my step 2 at the end.
Passing an Object or non primitive data for someone coming from C world was a little shocking.
Or as the man said, "Wooah".. funny teacher, I appreciate the effort ;) So basically the compiler doesn't even care (probably give you an error in newer java versions) what kind of Object you pass to the method. But for some reason it cant use that method unless you create an Object, throw the input into it while casting it to it-self!!! Does that even make sense??!!
Thanks for your reply, it's helping me reprocess the horror..
Grigorij Schleifer
10,365 PointsGrigorij Schleifer
10,365 PointsHi joseph,
I am an absolute programming beginner and Java is my first step into this deep and cold programming ocean. So "reprocessing the horror" of drowning is my daily driver and I am sure I will stay under water for a long long time yet ... BUT I thing I like it ... somehow :)
See you in the forum sailor !!!
Grigorij
joseph sack
3,539 Pointsjoseph sack
3,539 PointsWhat a great way to put it! Of course I enjoyed the "horror", it just cost me a whole couple of days! See you around, I did some C, assembly and discrete math, I'll be glad to answer any question I can on the basics. Easy sailing