Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Madni - 12 Jun 2006 12:22 GMT
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
Mike Schilling - 12 Jun 2006 12:55 GMT
> hi,
>
[quoted text clipped - 9 lines]
> -----------------------
> }

See the discussion of "enhanced for statement" in
http://java.sun.com/features/2003/05/bloch_qa.html.
Peter Van Weert - 12 Jun 2006 12:56 GMT
http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html

> hi,
>
[quoted text clipped - 13 lines]
>
> madni
Jussi Piitulainen - 12 Jun 2006 13:09 GMT
> 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.
Adam Maass - 12 Jun 2006 14:18 GMT
> hi,
>
[quoted text clipped - 9 lines]
> -----------------------
> }

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.