Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jyoti Vaskar
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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???
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jyoti Vaskar
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imagine this piece of code



No reason for an exception.
 
Jyoti Vaskar
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanku again..... !

for the explanation. I understood now!
 
sreekanth kulkarni
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sreekanth kulkarni
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for sharing the link..

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

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

 
Campbell Ritchie
Sheriff
Pie
Posts: 50232
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
You read it as
For each String s in args.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic