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

Marcus Green Mock

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question from Marcus Green Mock1

he gave this answer :
4) Exception raised: "java.lang.ArrayIndexOutOfBoundsException: 2"
but if you compile this code you will get morning
am I correct???
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
arg[0] = good
arg[1] = morning
arg[2] = out of bounds
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
zaghal
Did you compile it an run it? What did it do?
The arguements passed into a java program by the command line do not start with the class file name. In the String[] arguement to the main method the item at the 0 index is the 1st item entered on the commnad line after the file name. so in this case the item at 0 is good and the item at 1 is morning.
so you will get the index out of bounds Exception.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but if you compile this code you will get morning
am I correct???


The code compiles fine but raises an Exception of ArrayIndexOutofBounds...because arg[2] refers to the third argument which does not exist.
 
zaghal mohd
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
Zarina so we should say no4 even The code compiles fine and give answer morning
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Zaghal,
Notice that Marcus' fourth answer says that an exception will be raised. It does NOT say that there will be a compile time error. An ArrayIndexOutOfBounds exception is thrown at runtime, not compile time. The compiler has no way of knowing whether or not "2" is a valid index for the array. The problem happens at runtime. Does that help?
 
zaghal mohd
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Dave
I agree with you I just want to know why it take last argument
I will explain to you more
Ex: hi how are you
If you print this
System.out.println(argv[3]);
It will give =you.

If you say,
System.out.println(argv[4]); 4 and above
I will give= java.lang.ArrayIndexOutOfBoundsException.
That mean it�s accept the no from 0,1,2,3
Logically that not correct but if you write the code it will print to you what i mean it
And if you try
[0]=hi
[1]=how
[2]=are
[3]=you
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
zaghal
Why is that not correct? That is the way it is supposed to work.
The command line arguements are taken from the command line and put into the array of Strings that is passed into the main method. In Java all arrays start at 0 so the first arguement would be at index 0, the 2nd at index 1 and so on until the 4th arguement at index 3.
Are you confused about the class name not being part of the command line? That is the way Sun decided to implement it, it also makes sense in that the main method knows what class it is a member of so why bother passing it in.
I hope I understood your question correctly, if not let me know.
 
zaghal mohd
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Dave
you know Dave I think there is problem I compiled before and when I use System.out.println(argv[2]) he give me (morning) related to my first question very strange and now give me java.lang.ArrayIndexOutOfBoundsException
what you think..!!
 
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic