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 Packages

How does the putting "treet" in the System.out.printf line print the package name ?

import com.teamtreehouse.Treet; public class Example {

public static void main (String[] args) { Treet treet = new Treet(); System.out.printf("This is a new Treet: %s %n", treet);

}

}

Output: This is a new Treet: com.teamtreehouse.Treet@7852e922

I wanted to ask how in the last line of code, putting "treet" prints out the package name i.e., com.treehouse.Treet ?

1 Answer

andren
andren
28,558 Points

When you pass printf an object it will automatically call that objects toString method, at least when the %s placeholder is used. By default an object's toString method will simply print out the name of the class along with it's hashcode. The default toString method basically looks like this:

public static String toString(Object o) {
    return o.getClass().getName() + "@" + Integer.toHexString(o.hashCode());
}

So it's due to that method being called that you end up with having the class name (which includes the package info) printed out.

Thank you so much andren. I get it now.