• Post Reply Bookmark Topic Watch Topic
  • New Topic

args  RSS feed

 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

false
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

I ran this code thru IDE.My questions are 1) args is not null then what object it has ? 2) basic for loop causes runtime error then why enhanced for loop doesn't ?
at TestArray.main(TestArray.java:11)
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because your loop ending condition is incorrect. You're accessing the 0-th element even though there is no such element.
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Because your loop ending condition is incorrect. You're accessing the 0-th element even though there is no such an element.


Why enhanced for statement doesn't throw runtime error ? and args is not null then what it refers ? thank you
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is String[] args=new String[0]; but don't know how enhanced work with this ,anyways thank you.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf is correct. You are using <= when you ought to use <
If you pass a zero‑length array as args (i.e. write nothing), you will get nothing printed out. The enhanced for loop is not prone to that sort of error. You cannot redeclare args like that.
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that' right
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!