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

Serialization: What does the Treet[] treets = new Treet[0] mean?

In the Serialization video, Craigs writes Treet[] treets = new Treet[0];

I don't understand why he wrote new Treet[0]? What does that mean? I am only familiar with creating arrays by saying Treet[] treets = {treet1, treet2, treet3};

Also, how do I post questions into the correct category?

Craig Dennis
Craig Dennis
Treehouse Teacher

If you ask a question from the video it will be properly categorized...adding an answer too.

1 Answer

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Array sizes are locked at creation. This is how you state how large the array should be so if I you write:

String[] words = String[3];

It will be a 3 element array of null values.

Make sense?

Thanks again, you tha man!

Actually, just to be sure.

So in the video's example: Treet[] treets = new Treet[0]; That means create a new Treet array with 1 element? Since arrays are 0-indexed?

Also, wouldn't String[3] create a 4 element array of null values? Since you'll have words[0], words[1], words[2], words[3]?

Craig Dennis
Craig Dennis
Treehouse Teacher

Nope that's actually the size. The Treet array has 0 elements. I know...confusing :/