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

Kevin Lankford
Kevin Lankford
1,983 Points

Casting object as a String--I may not be fully understanding this question

but are we to be creating a conditional statement to determine whether this is in fact a String? How would I utilize instanceof in that fashion?

if (obj instanceof String){ result = (String)obj }

return result;

Not quite sure how to approach this problem, but the instructions gave me this impression

com/example/BlogPost.java
package com.example;

import java.util.Date;

public class BlogPost {
    private String mAuthor;
    private String mTitle;
    private String mBody;
    private String mCategory;
    private Date mCreationDate;

    public BlogPost(String author, String title, String body, String category, Date creationDate) {
      mAuthor = author;
      mTitle = title;
      mBody = body;
      mCategory = category;
      mCreationDate = creationDate;
    }

    public String getAuthor() {
      return mAuthor;
    }

    public String getTitle() {
      return mTitle;
    }

    public String getBody() {
      return mBody;
    }

    public String getCategory() {
      return mCategory;
    }

    public Date getCreationDate() {
      return mCreationDate;
    }
}
TypeCastChecker.java
import com.example.BlogPost;

public class TypeCastChecker {
  /***************
  I have provided 2 hints for this challenge.
  Change `false` to `true` in one line below, then click the "Check work" button to see the hint.
  NOTE: You must set all the hints to false to complete the exercise.
  ****************/
  public static boolean HINT_1_ENABLED = false;
  public static boolean HINT_2_ENABLED = false;

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

3 Answers

Benjamin Barslev Nielsen
Benjamin Barslev Nielsen
18,958 Points

Your understanding is correct and the suggestion is correct for task 1 except a semicolon is missing after (String) obj.

Kevin Lankford
Kevin Lankford
1,983 Points

I've added this condition, but it the compiler error I'm getting looks like something may be missing before returning the result:

if (obj instanceof String){ String result = (String)obj; } else { Object result = obj; }

return result;

./TypeCastChecker.java:19: error: cannot find symbol return result; ^ symbol: variable result location: class TypeCastChecker 1 error

Benjamin Barslev Nielsen
Benjamin Barslev Nielsen
18,958 Points

Sorry for the late reply but here it comes. Because of Javas scope rules, if you declare a variable inside { ... } the variable is only accessible in that block of code. Therefore you cannot access result outside the if-statement, if you declare it in the branches of the if-statement. Therefore you still need to declare the variable before the if-statement:

String result = "";

Remember that the return type is String, so we would always return a String, not a Object, so therefore you should not declare result with the type of Object. If obj is not a String, we know that it is a blogpost and therefore we can cast obj to BlogPost, and then assign that BlogPost's title to result, and afterwards returning it:

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

Hope this helps

Kevin Lankford
Kevin Lankford
1,983 Points

This definitely helped my figure it out, thank you! One more quick question, what in the rules of Java make ((BlogPost)obj).getTitle(); correct and not (BlogPost)obj.getTitle(); ?

Benjamin Barslev Nielsen
Benjamin Barslev Nielsen
18,958 Points

Let's first look at the incorrect code:

(BlogPost) obj.getTitle();

In this code we cast obj.getTitle() to BlogPost, but this raises two problems. obj is still of type Object, so we do not know that it has an getTitle method, and obj.getTitle() returns a String, so casting this to BlogPost will be wrong.

In the correct code:

((Blogpost) obj).getTitle

we cast obj to BlogPost, and since obj is a BlogPost, we know it has a getTitle method, so this is correct.

Kevin Lankford
Kevin Lankford
1,983 Points

Thank you for explaining that, it makes sense to me now! :)