File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q. Method main()

 
Karin Paola Illuminate
Ranch Hand
Posts: 109
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.tipsmart.com/javacert/ptp/basics.htm
Q20.What will be the result of compiling following code

(1) Code will not compile and will give "Duplicate main() method declaration" error
(2) Code will compile correctly but will give a runtime exception
(3) Code will compile correctly and will print "In first main()" (without quotes) when run with argument of 'a'.
(4) Code will compile correctly and will print "a" (without quotes) when run with argument of 'a'.
SKIP THE QUESTION
Their answer: 3
Why will this run with argument of 'a'?
[ Jess added UBB [code] tags to preserve whitespace, check 'em out! ]
[ January 06, 2003: Message edited by: Jessica Sant ]
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you call this program:
java MyClass any arguments can go here
the JVM looks for a method who's signature looks like this:
public static void main( String[] )
and that's the method that it will call.
the fact that you have overloaded the main() method to have another signature as well is inconsiquential to the JVM:
public static void main( char[] )
is a totally different method -- and the JVM won't call it by itself. You can call it if you want to... but the JVM will only call the one who's arguments is String[]
The fact that you started the program with the argument 'a' won't affect which method is used either -- in fact you can call it with however many agruments you like... they'll each be stored in that String array
[ January 06, 2003: Message edited by: Jessica Sant ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Executing a Java program isn't exactly like invoking a function within a Java program. There are a couple things to keep in mind.
First of all, when you invoke a Java program, the JVM looks into the class you want to run for a method with the following signature:

If it finds that method, it executes it, passing any parameters you've supplied in the String array. If it does not find that method, it will issue an error.
The JVM will never execute the method:

automatically. That doesn't mean you couldn't invoke it yourself, explicitly, but that's not what the JVM will do when you execute the program. It doesn't check to see what types of paramaters you've provided, it just interprets them all as strings. Even had you provided a number such as 2, it still would have considered that a String.
I hope that helps, but you can check out the JLS, §Execution for more details.
Corey
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic