• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Explain me please

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone please let me know the output of both...
executed by entering "java test lets see what happens".
1.public class test {
public static void main(String args[]) {
System.out.println(args[0]+" "+args[args.length-1]);
}
}

Answer for this is:lets happens

2.
public class test {
public static void main(String args[]) {
System.out.println(args[0]+" "+args[args.length]);
}
}


answer is:The program will throw an ArrayIndexOutOfBounds exception
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The array indexes are zero based. For the first program the args.length variable gives you a value whose index is out of bounds. args.length - 1 will give you the last element in the array and args[0] gives you the first element in the array ( considering that you pass atleast one element in the command line).

Example: java test 1 2

here args[0] = 1 and args[1] = 2 and args.length = 2 so args.length-1 gives you the value of the last element.
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I was obviously writing this at the same time as John...

Anyone please let me know the output of both...



Since you've supplied the output, I assume you actually don't mean that! As far as explanation goes:

In the first example, there are 4 command line arguments { "lets", "see", "what", "happens" }, but these are referenced on a 0-based index, so in fact although there are 4 entries, they are actually indexed by 0, 1, 2, 3. So in the println() argument we have:

args[0] which references "lets" as the first entry
args[args.length - 1] == args[4-1] == args[3] which references "happens"

So you see, the last index is the length of the array subtract 1.

In the second example, you attempt to access args[args.length], but this is always out of range, because the last index is always (args.length - 1) and not args.length.

Using the previous command line data we would be trying to reference:

args[args.length] == args[4] which does not exist!

Therefore an ArrayIndexOutOfBounds is thrown. Does that make sense? If it's array indexing that troubling you, remember that arrays are 0-based, not 1-based as our counting usually is; we count 1, 2, 3..., arrays count 0, 1, 2... and so the last index is one less than the length.
[ April 16, 2006: Message edited by: Charles Lyons ]
 
surekha raju
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for ur explanation...i wont go wrong in future...
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic