*
The moose likes Beginning Java and the fly likes For (String s:ar)   loop................. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "For (String s:ar)   loop................." Watch "For (String s:ar)   loop................." New topic
Author

For (String s:ar) loop.................

Jyoti Vaskar
Ranch Hand

Joined: Jun 30, 2009
Posts: 142

if " for (String s:args) " for loop is used and no values passed at command line for args[].

the loop is not getting executed & also it does not throw any exceptions???Why???


thanks
Jyo
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Starting a Java program with no arguments (as discussed in the thread before) results in new String[0] for args, hence args is a valid Array, the loop does have nothing to iterate over, and also has no reason to throw Exceptions.


JDBCSupport - An easy to use, light-weight JDBC framework -
Jyoti Vaskar
Ranch Hand

Joined: Jun 30, 2009
Posts: 142
Sebastian Janisch wrote:the loop does have nothing to iterate over, and also has no reason to throw Exceptions.


But as args[0] has no value to pass to string 's' I was expecting NullPointerException?
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Imagine this piece of code



No reason for an exception.
Jyoti Vaskar
Ranch Hand

Joined: Jun 30, 2009
Posts: 142
thanku again..... !

for the explanation. I understood now!
sreekanth kulkarni
Greenhorn

Joined: Oct 19, 2013
Posts: 2
Could not understand how " for (String s:args) " would be read...

is it similar to assigning value to variable s i,e s=args[0];

if so where is size declaration for args... this seems to be silly but just wanted to understand the syntax: for (String s:args)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

sreekanth kulkarni wrote:Could not understand how " for (String s:args) " would be read...
is it similar to assigning value to variable s i,e s=args[0];
if so where is size declaration for args... this seems to be silly but just wanted to understand the syntax: for (String s:args)

I'm not quite sure what you're having trouble with. The construct is called a "for-each" loop, and was introduced in version 5 for doing precisely what you see without the need for indexes.

As to size declarations: why would you need one? The array (args) already has a size.

Perhaps you could elaborate on exactly what it is you don't understand.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
sreekanth kulkarni
Greenhorn

Joined: Oct 19, 2013
Posts: 2
Thanks for sharing the link..

Now i understand, "for(String a: args){}"

means "a" gets successively each value in "args".

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
Welcome to the Ranch
You read it as
For each String s in args.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: For (String s:ar) loop.................
 
Similar Threads
Comparing two Strings
string reversal problem
Doubt in nullpointerException.
Finding exact element in a javascript variable
Why does cons remain equal to null?