Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ":" mean in this Java statement?

for (Season time : Season.values() )
system.out.println (time+ "\t" + time.getSpan());

I see an example for enumeration using :. What does this mean?

like image 483
Strawberry Avatar asked Aug 07 '26 23:08

Strawberry


2 Answers

This is the Java syntax for a foreach loop.

The loop will iterate over all the items in the collection of objects returned by Season.values() one at a time, putting each item in turn into the time variable before executing the loop body. See this closely related question for more details on how the foreach loop works.

like image 190
Cameron Avatar answered Aug 10 '26 12:08

Cameron


It is just a token to separate the iterating variable on the left from the array on the right in the new for-each loop

like image 43
codaddict Avatar answered Aug 10 '26 12:08

codaddict



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!