Why do people leave unused `String args[]` in Java? [duplicate] - java

This question already has answers here:
Why is String[] args required in Java?
(6 answers)
Closed 9 years ago.
When I see Java programs, many leave the String args[] on even though the program doesn't use them. Why is this? Anything in particular?

String args[] is part of the method signature for main. If you don't have it you will get the exception below when you try and run the code.
Exception in thread "main" java.lang.NoSuchMethodError: main

If it is for a main(String[]) it is to fulfill the method signature & therefore vital.

Its required by the specification.
The method main must be declared public, static, and void. It must
specify a formal parameter (§8.4.1) whose declared type is array of
String. Therefore, either of the following declarations is acceptable:
public static void main(String[] args)
public static void main(String... args)
Specification

The code within the main may not directly use it, but it is still required. 'String args[]' is where any command line arguments are passed. Even if you pass in 0 arguments, there needs to be a way for that to be verified. It is also the required signature for main by the requirements of the JVM.

Related

Difference of args vs arg in main-method, java? [duplicate]

This question already has answers here:
Difference between String[] arg and String[] args?
(7 answers)
Closed 2 years ago.
Why does
" public static void main(String[] arg) { "
work instead of
"public static void main(String[] args) { " ?
Does it have anything to do with the String-Array?
And how exactly does it work?
May using "arg" instead of "args" just be a tiny bit more efficient?
There is absolutely no difference, beyond the normal differences of parameter names (as in, you have to use the right name within the method body). The name of the parameter to the main method is entirely by convention. It's entirely valid to write:
public static void main(String[] dontUseJavaUtilDate)
as the declaration for your entry point.
From JLS 12.1.4 which uses Test
Finally, after completion of the initialization for class Test (during which other consequential loading, linking, and initializing may have occurred), the method main of Test is invoked.
The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String.
There's no specification of the parameter name.
Ditto from the JVM specification section 5.2:
The Java Virtual Machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (§5.3.1). The Java Virtual Machine then links the initial class, initializes it, and invokes the public class method void main(String[]). The invocation of this method drives all further execution. Execution of the Java Virtual Machine instructions constituting the main method may cause linking (and consequently creation) of additional classes and interfaces, as well as invocation of additional methods.

What does (String... arg) mean in a function definition? [duplicate]

This question already has answers here:
What do 3 dots next to a parameter type mean in Java?
(9 answers)
Closed 7 years ago.
public static void main (String... arg)
I have never encountered the ... part in the function definition. Could anyone give some insight into this notation?
The ... indicates that you are passing 0 or more arguments of the type and the method will access them as an array of objects of the type. You may pass them as an array or as a sequence of objects of the declared type.
I.e.:
In your main method use
String firstArg = arg[0];
to access the first argument.
Look at the documentation of varargs for more info.

Main method in java only accept String[]

Object is the super type of all classes in Java. Consider my following class
public class Test {
public static void main1(Object[] args) {
System.out.println("I accept an object array");
}
public static void main(String[] args) {
main1(args);
}
}
Due to object superiority object array can accept any object type arrays. But Still java doesn't consider following class contains a main method.
public class Test {
public static void main(Object[] args) {
}
}
Why java never give this opportunity while object is ultimate supper type for all classes in java.
because java looks explicitly for public static void main(String[] args) when running.
specified in 12.1.4 of the jls
The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String. Therefore, either of the following declarations is acceptable:
Object wouldn't make sense, because you can not pass an other object through the console.
The main Method of Java is specified with strings as parameters. So the compiler can't detect the main-method with objects as args. I guess the resaon for this is, that the main method are usally called due an console command, and there you have only the opportunity to set strings as parameters.
The String[] were for command-line arguments, strings are what the user enters at the command line. Objects can't be entered from Command line.
From JLS:
The method main must be declared public, static, and void. It must
specify a formal parameter whose declared type is array of String.
Therefore, either of the following declarations is acceptable:
public static void main(String[] args)
public static void main(String... args)
there are a few ways to answer this
"because". the main() entry point to a program is specified like this and canot be overloaded to accept other arguments
as hinted by assylias, the main() method is expected to be invoked from a comamnd line or shell. this means that the arguements will always be strings. whatever you type on a command line is a string.
One point as all explain there is no way to pass object from console so it's meaningless.
Still as I also think Object is super class so why jvm does not understand it, but there is also other point that if jvm allowed to accept Object arguments than user can pass non-string variable as well so there jvm will create error that's why I think jvm make restrict to pass string variable.
JVM calls main() as a thread. When we call main() from another method in java, we call it as function or method.
Actually the confusion here is " why auto casting is not applicable when we try to call main(Object args) from console."
May be this restriction has been put in native methods of JVM to avoid the complications.
I you guys observe same case u will find for
catch(Object e)
Summery line: it is the native methods code of JVM which restricts the auto casting, to avoid complications.
The arguments passed to the main method are from command line. So they are String
main method can also be written like this
public static void main(String... args) {
}

String... parameter in Java [duplicate]

This question already has answers here:
varargs and the '...' argument
(3 answers)
Closed 9 years ago.
I have to implement an API for a homework assignment, and my instructor has used a notation I am unfamiliar with for one of the methods in the API (javadoc based).
public void method(String... strs);
What do the '...' mean? It later looks like I'll need to call this same method using a single string actual parameter, as well as multiple string actual parameters...
Java doesn't have optional arguments (to my knowledge), so I am a little confused here...
It's called varargs; http://docs.oracle.com/javase/6/docs/technotes/guides/language/varargs.html
It means you can pass an arbitrary number of arguments to the method (even zero).
In the method, the arguments will automatically be put in an array of the specified type, that you use to access the individual arguments.
Yes, that means you can take arbitrary no of Strings as an argument for this method.
For your method:
public void method(String... strs);
You can call it as:
method(str)
method(str1, str2)
method(str1,str2,str3)
Any no of arguments would work. In other words, it is a replacement for:
public void method(String[] str);
It's called an ellipsis and it means the method can take multple Strings as its argument.
See: The Java tutorial on passing arguments on Oracle's site.
See java optional parameters : as of Java 5, Java has support for variable numbers of arguments.

What does string[] args mean in public static void main(string[] args) in java? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does “String[] args” contain in java?
I want to know the significance of the term written inside the bracket in definition of main function in java i.e.string[] args. What does it mean in public static void main(string[] args)? Is it always necessary to write it?
More over how many engines mysql server has and what is the default engine?
Public -> makes member accessible outside the class
static -> allows main( ) to be called without having to instantiate a particular instance of the class
void -> main() doesnt return value
main(0 -> it is the method called when a Java application begins
string[] args -> Its parameters. String args[ ] declares a parameter named args, which is an array of instances of the class String. In this case, args receives any command-line arguments present when the program is executed.
public class MainClass{
public static void main(String[] args){}
}
When we run in the command line
java MainClass
The JVM here tries to find a method main, We are basically accessing the method main outside the class and package so the method is Public.
We are running this program by referring the class name and we are not creating any object,so to access the method without creating an instance it has to be static. JVM does not handle the output of the method and hence the return type is void.
We can pass series of arguments through command line and String can enclose all the primitive type in Java and we do not know the number of arguments that can be passed so it declared with String array.
For MySQL engine types please refer the link Engines
The Java contruct for main() is the equivalent of C's "void main(int argc, char **argv)". Java's main() receives an array of strings, from which one can obtain the length with args.length; no need for argc, the count. It can also be written "String args[]"; both ways specify an array of strings.
The mysql question rightly belongs in another SO question altogether.
As written , string shows that args variable is of type String and [] shows args is an array.
Inside main() is because, Main() is called whenever program is executed, So this args will hold everything you give it at run time i.e. at the call of main()

Categories