aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q. Method main() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q. Method main()" Watch "Q. Method main()" New topic
Author

Q. Method main()

Karin Paola Illuminate
Ranch Hand

Joined: Oct 18, 2002
Posts: 109
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 ]

I not only use all the brains that I have, but all that I can borrow. [Laurence J. Peter]
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

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 ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q. Method main()