Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1249 :

Posted: Sun Feb 22, 2015 12:46 pm
by giorgiadiro
I have a doubt about the fact that main method accepts only a String Array parameter.
It's what I always knew, but in Mala Gupta Java 7 Certification Guide it's written:

The method must accept a method argument of a String array or a variable
argument of type String.


and

It’s valid to define the method parameter passed to the main method as a variable
argument (varargs) of type String:
public static void main(String... args)


so, is it totally wrong???

thanks

Re: About Question enthuware.ocajp.i.v7.2.1249 :

Posted: Sun Feb 22, 2015 6:29 pm
by admin
String[] and String... are same.
When you use String... the compiler allows you to pass any number of String arguments to that method but internally,
compiler converts String... to String[]. It also wraps the arguments into a String[] and invokes the String[] method. The JVM has no idea about String... It sees only String[].

The explanation has now been enhanced to include this point.

Re: About Question enthuware.ocajp.i.v7.2.1249 :

Posted: Sun Mar 01, 2015 1:45 pm
by giorgiadiro
I didn't know that the compiler should convert String... to String[].

thank you for the explanation!

Re: About Question enthuware.ocajp.i.v7.2.1249 :

Posted: Wed Oct 14, 2015 7:54 pm
by andrix
Just a little note about explanation of this question.

It is stated: "...Therefore, an exception will be thrown at runtime saying no main(String[] ) method found."
I find this somewhat misleading because "exception" can be understood here as java Exception. The problem is that actually Error (java.lang.NoSuchMethodError) will be thrown which does not extend Exception class. It extends java.lang.Throwable.

I got confused (for split second ofc ;) ) reading this so... I just say.

Re: About Question enthuware.ocajp.i.v7.2.1249 :

Posted: Wed Oct 14, 2015 9:41 pm
by admin
It is a common practice to call all sorts of Throwables as "exceptions" (with a small e). It does not imply the Exception class.
You will see the same practice followed in the real exam as well. Now that you have already encountered it, you will not get confused in the real exam :)

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1249 :

Posted: Wed Jun 26, 2019 12:47 pm
by BaxLi007
Just purchase the license and make BASIC test .
The strange issue that on question with enthuware.ocajp.i.v8,2,1249 I was redirected here (but ok not the issue) ...
The issue is definition:
- public static void main(String args) (?) where is vararg(...) here ????

Re: About Question enthuware.ocajp.i.v7.2.1249 :

Posted: Wed Jun 26, 2019 1:01 pm
by admin
There is no ... in the code given in the problem statement. That is why the correct answer is "None of these".

Re: About Question enthuware.ocajp.i.v7.2.1249 :

Posted: Wed Jun 26, 2019 1:02 pm
by admin
BaxLi007 wrote:
Wed Jun 26, 2019 12:47 pm
The strange issue that on question with enthuware.ocajp.i.v8,2,1249 I was redirected here (but ok not the issue) ...
It is by design. The same question appears in v7 as well as in v8. That is why it is redirecting to the same thread.