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 - Retired Organizing Data Splitting Strings

Yogesh Ravi
Yogesh Ravi
1,028 Points

Cannot understand the concept of word

Is word a variable that stores each of the words in the String? If so why it is not declared?

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I feel like you might be referring to this for loop in the video.

for(String word: treet.getWords()) {
    System.out.println(word);
}

For loops are a bit special. The very first thing in there which is String word is a declaration of a variable of type string. And what it's saying is that for each iteration it's going to pull out exactly one item from a collection and assign it to a local variable word. The println then uses this variable to display the value of word and proceeds on to the next iteration until the collection is exhausted.

However, the scope of this variable is extremely limited. The second the for loop stops running, that variable will no longer be accessible.

Hope this helps! :sparkles:

Yogesh Ravi
Yogesh Ravi
1,028 Points

Thank you so much, this clear explanation made me understand.