File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array!

 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will the following give runtime exception?
int i=0;
while (i++ < args.length) {
System.out.print(args[i]);
}
Since the condition is checkd for i less than args.length, it won't..rite?Can anybody explain pl!Thanks
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does a simple main method containing that code do when you run it?
Have you tried it? Or try to mentally simulate the code with an array of one string.
[ May 11, 2004: Message edited by: Barry Gaunt ]
 
Shilpi M Ag
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont think it should give an error
 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the command line arguments passed is A B C D and the answer they have given is that it gives run time exception.It shouldn't be correct rite?
 
Sharad Goel
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should give Runtime Exception.
Reason:
At first iteration 0 < 4, and i = 1
At Second iteration 1 < 4, and 1 = 2
At Third iteration 2 < 4, and i = 3
At Fourth iteration 3 < 4, an i = 4
So in fourth iteration, it will try to get a value from array at location 4 where as the size of array is 4, hence runtime exception.
Hope I am clear.
Cheers~
Sharad
 
Jong Limb
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The while loop checks i++ which returns the value of i and then increments it. So after the i == (args.length - 1) test returns true, i will equal args.length. Since you can only reference args[0] - args[args.length - 1], you will get an array out of bounds exception.
 
Ana Abrantes
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 int i=0;
2 while (i++ < args.length) {
3 System.out.print(args[i]);
4 }
In line 2 i has a value and in line 3 it has another.
So, you pass the test in the while, but you print the next element of args.
That's why you got a runtime exception.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when written after the variable, the "++" operator fires AFTER the other stuff on the line is done. so we go around the loop a few times. eventually, we enter the loop with i equalling 3. the first thing that happens is we to the test. since args.length() is 4, we pass.
AFTER WE DO THAT TEST, we increment i to 4. you now try and get the element at index 4, which throws the exception.
You should always try running code like this when what the author says doesn't make sense. if there is an exception thrown, take out the line and try and figure out why. you could take out the "args[i]" and just print i. you'd see it printing values 1,2,3 and 4. then you can try and figure out why it's that instead of 0,1,2,3
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by fred rosenberger:

You should always try running code like this when what the author says doesn't make sense. if there is an exception thrown, take out the line and try and figure out why. you could take out the "args[i]" and just print i. you'd see it printing values 1,2,3 and 4. then you can try and figure out why it's that instead of 0,1,2,3

I tried earlier to get him to do that, Fred. But I guess he's a theoretical programmer
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic