• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

problem in public static void main(String str [])

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


in case of args its work but for asd its not working . ??
why String[] args work without initalization ???
 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please be sure to take the time to compose descriptive subjects for your posts; read this for more information.

A title such as "explain" is not helpful -- and in fact, comes across as rather rude. What would happen if all posts had such a title?

Please go back and change your post to add a more meaningful subject by clicking the button on your post.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vineet walia wrote:
why String[] args work without initalization ???



There is no way that this...



will work. Once you set args to null, you lose access to the string array that is passed to the main() method. And you should get a NPE as soon as you try to dereference it on the next line.

Henry
 
vineet walia
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

vineet walia wrote:
why String[] args work without initalization ???



There is no way that this...



will work. Once you set args to null, you lose access to the string array that is passed to the main() method. And you should get a NPE as soon as you try to dereference it on the next line.

Henry



what i am asking is if we have have to create array we do like this
String [] st= new String[5];
either we have to cerate class of String using new and gave reference st

but in case public static void main(String [] str)
we can assign value to str
like str[0]="aaaaa";
without new etc
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but in case public static void main(String [] str)
we can assign value to str
like str[0]="aaaaa";
without new etc



That's because "str" is a parameter -- the caller of the main() method instantiated the array, and filled in with the "parameters" of the program. Technically, you shouldn't be changing it.

Furthermore, the size of the array is based on the number of parameters -- so... if you call the program with no parameters, index zero is invalid... meaning that you will get an array index out of bounds exception.

Henry
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

we have have to create array we do like this
String [] st= new String[5];
either we have to cerate class of String using new and gave reference st



then we can assign values to the index positions like this st[0]....st[4](any 1) = "something";

but in case public static void main(String [] str)



What happens here is when in the command line if you don't pass any arguments then in the background this gets executed like this String[] str = new String[0]. So then syntactically str[0] = "something" is proper but at runtime there is no 0 index in the string object. Therefore you will get a ArrayIndexOutOfBoundException.

Zero length arrays are valid in java.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic