whats use of ":" in java in the following code ?

M

Madni

hi,

here is the code sample which is using : operator to call some class
methods.

for ( classname obj:somemethod() ) // here is the problem in
understanding
{

-----------------------
------------------------
-----------------------
}

regards

madni
 
J

Jussi Piitulainen

Madni said:
here is the code sample which is using : operator to call some class
methods.

for ( classname obj:somemethod() ) // here is the problem in

Here's an authoritative explanation:

<http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html>

I knew it was new in Java 1.5, so I googled for what's new in Java
1.5., and there it was; "for" might be a difficult word to find.

The mere answer is that

for (c o : m()) { ... }

iterates over a collection of values returned by the method m, binding
o to each of them in turn. The collection can be an array or a
Collection.

I recommmend spaces around the colon.
 
A

Adam Maass

Madni said:
hi,

here is the code sample which is using : operator to call some class
methods.

for ( classname obj:somemethod() ) // here is the problem in
understanding
{

That's the syntax for the 'foreach' style for loop.


somemethod() returns a Collection (or other Iterable) or an array. classname
is the name of the class contained in such Collection or array. obj is the
loop variable. The code executes the body of the loop once for each element
in the array, supplying the current element in the variable obj.

-- Adam Maass
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top