• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

K&B-Threads Chapter Q 9 - main(args) not null?

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 9 of Chapter 9

1. public class WaitTest {
2. public static void main(String [] args) {
3. System.out.print("1 ") ;
4. synchronized(args) {
5. System.out.print("2 " ) ;
6. try {
7. args.wait();
8. }
9. catch(InterruptedException e){}
10. }
11. System.out.print("3 ");
12. }
13. }

Answer 1 2

Does that mean that main's argument "args" was not null even though NO VALUE WAS PASSED WHILE CALLING THIS CLASS?

IS "args" of main is ALWAYS NON NULL?!
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
args of main() is an array containing zero elements, Not null.
 
Ajay Chandravadiya
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does is it mean that whenever you say "java SomeClass" it invokes main() method with an Empty but Initialized arrays (like --String[] args = String[0]--)

Any official document/reference that states that JVM behaves this way?
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you try to run a program using

java SomeClass

and dont give any arguments to it then,


the JVM will not even assign any space to the array args

and when you try to access args[0] here you will get an ArrayIndexOutofBoundsException

Mary
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if the program is executed without any arguments, the 'args' is not null. In such case it will be initialized to an array of Strings containing zero elements.
 
lowercase baba
Posts: 13081
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the "String [] args" is like an egg carton. it is always handed to the main method.

how many eggs/strings are inside it is determined by what you put on the command line.
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic