• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

q on primitives

 
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class JMM101 {
public static void main(String[] args) {
int i = 0;
while (i++ < args.length) {
System.out.print(args[i]);
}}}
java JMM101 A B C D E F
What is the result of attempting to compile and run the program using the specified command line?

a. Prints: JMM101ABCDEF
b. Prints: ABCDEF
c. Compile-time error
d. Run-time error
e. None of the above


I answered b, but the correct ans is d.
In the while loop, we take the value of i, compare the same and then increment right?
wht wud be the effect if it was while(++i,args.length)?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BCDEF
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The result will be BCDE.

Thanks.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Edoil wrote:

The result will be BCDE.

Thanks.



I executed the code myself.

With i++ you get an ArrayIndexOutOfBoundsException.

With ++i you get BCDEF.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is the result an ArrayIndexOutOfBound Exception? Please explain.
Thanks
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You get the ArrayOutOfBoundsException because:
arg.length = 6
When you get to the situation where i=5, you move into the while loop and i gets incremented to 6. However, arrays are indexed beginning at 0, so the six elements indexes range from 0-5. Hence when you try to access element 6, you get an ArrayOutOfBoundsException.

Hopefully I explained this well!
 
Erin Goettl
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Erin Goettl:
You get the ArrayOutOfBoundsException because:
arg.length = 6
When you get to the situation where i=5, you move into the while loop and i gets incremented to 6. However, arrays are indexed beginning at 0, so the six elements indexes range from 0-5. Hence when you try to access element 6, you get an ArrayOutOfBoundsException.

Hopefully I explained this well!



Whoops I meant args.length :roll:
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sumitabh,

The above code can be manipulated as follows

I've modified the code but maintained the functionlity to give you better understanding how post increment works in this case.
Try walking through this code and you will figure out why we get exception at runtime.
 
reply
    Bookmark Topic Watch Topic
  • New Topic