String... parameter in Java [duplicate] - java

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.

Related

What is the best way to send a method a dynamic number of variables? [duplicate]

This question already has answers here:
Can I pass an array as arguments to a method with variable arguments in Java?
(5 answers)
Closed 6 years ago.
I have a java program with a method that will consistently receive a different number of int values. What is the best way to send it the ints?
A construct called varargs (or an arbitrary number of arguments) helps you.
method(int... ints) { ... }
Then, varargs will be turned into int[] by the compiler.
OK, how to call those methods?
method(1, 2, 3);
method(new int[]{1, 2, 3});
About the question in the comments:
method(Arrays.stream(yourStringArray).mapToInt(Integer::parseInt).toArray());
You may convert String[] to int[] firstly, and then pass the result to the method.
Something like below you can do, making use of dot notation for method declaration with multiple arguments
public TestClass
{
public void process(int... variables)
{
// process your variables,
// Iterate through variables
}
}
Plus refer this, Java multiple arguments dot notation - Varargs

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.

Checking for valid overloading [duplicate]

This question already has answers here:
Which overload will get selected for null in Java?
(3 answers)
Closed 8 years ago.
I want to know whether this is a valid overloading :
public class OverLoadingTest{
private void callFunction(Object object){
System.out.println("Printing Object");
}
private void callFunction(String string){
System.out.println("Printing String");
}
}
Further more, since someone asked me this question.
If I do like this,
OverLoadingTest test = new OverLoadingTest();
test.callFunction(null);
what will be printed ?
Of course my opinion is that it isn't valid overloading at all.
So no question of the second part.
Please tell me about this with some explanation.
The method with the least generic argument is called. So, in your case, it will be method accepting String
Note : If 2 classes are at the same level, then you will get an ambiguous call exception. For example if one method took String and another took Exception.
If more than one member method is both accessible and applicable to a method
invocation, it is necessary to choose one to provide the descriptor for
the run-time method dispatch.
The Java programming language uses the rule that the most specific method is chosen.
See more details in JSL 15.12.2.5
In your case, String method will be invoked, if argument is String or null and for other argument's types Object method will be invoked.
In your example, if you define one more method with argument type that is not String (e.g Integer), can't compile the source as it is ambiguous to be invoked between the methods with String and Integer as they are same level.

purpose of String args[] in main method in java [duplicate]

This question already has answers here:
What is the "String args[]" parameter in the main method?
(18 answers)
Closed 5 years ago.
In java main method, what's the purpose of String args[] in
public static void main (String args[]) ?
The main method has only one because it's a form for standardisation. Oracle does not know how many arguments a programmer will need and what types of them. For this reason, with the args[] of type String you can pass N arguments to your program. You can then parse it to any primitive type in Java.
Here is an example of passing arguments to an application with Java:
java MyApp arg1 arg2 arg3 arg4 ... argN
Every value passed is separated by spaces and based in the position, you can retrieve and manipulate them, for example, if you need the arg at position 4 and convert it to a double, you can do this:
String toDouble = args[4];
double numericalValue = Double.parseDouble(toDouble);
Also, this form was thought to pass some parameters to define and configure some behaviour of your application, so with an unique array this can be accomplished.
args[] is an String array. So you can pass more than one String to your method:
args[0] = "Hello";
args[1] = "World";
...
When you run your program from the command line or specify program arguments in your IDE, those arguments are split by spaces ("hello world" becomes "hello" and "world", for example) and given to your program in the first argument. Even if the other array existed, there would be no use for it.
You are questioning why Java (and at the same time C/C++) Main threads have only two arguments. Java class has a requirement to use the main method defined in the main class. You would see an error otherwise:
"The program compiled successfully, but main class was not found.
Main class should contain method: public static void main (String[] args)."
This is just a Java requirement you need to adhere to (under silent protest if you wish).
As opposed to that, in C/C++/C++11 you need to have an int main method, but need not have any arugment. i.e. the following is allowed by the C++ main class requirement:
#include <iostream>
int main(void) { return 0; }
Note that in C/C++, you can also pass an integer as argument in the main.
#include <iostream>
int main(int argc){ return 0;}
To summarise, C/C++/Java each have got their own standards which you have to adhere to. You can argue that something isn't right, but as long as you are one of those people who are in the standards committe, I don't think you can change anything much.
A question regarding C main has been answered here. Also, the necessity of the main method with String[] args has probably been explained here already.

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

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.

Categories