wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array!" Watch "Array!" New topic
Author

Array!

Sridhar Srinivasan
Ranch Hand

Joined: Nov 07, 2003
Posts: 117
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


Software_guy
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Shilpi M Ag
Ranch Hand

Joined: May 07, 2004
Posts: 40
i dont think it should give an error
Sridhar Srinivasan
Ranch Hand

Joined: Nov 07, 2003
Posts: 117
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

Joined: Oct 28, 2003
Posts: 8
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

Joined: May 08, 2004
Posts: 12
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

Joined: Sep 04, 2003
Posts: 43
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.


Ana<p>SCJP 1.4
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11250
    
  16

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


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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
 
subject: Array!