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

Arnold Rosario
Arnold Rosario
16,357 Points

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};

Arnold Rosario
Arnold Rosario
16,357 Points

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?

Arnold Rosario
Arnold Rosario
16,357 Points

Thanks again, you tha man!

Arnold Rosario
Arnold Rosario
16,357 Points

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?

Arnold Rosario
Arnold Rosario
16,357 Points

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 :/