(String ...) is an array of parameters of type String, where as String[] is a single parameter.
Now here String[] can full fill the same purpose here but (String ...) provides more readability and easiness to use.
It also provides an option that we can pass multiple array of String rather than a single one using String[].
(String ...) is an array of parameters of type String, where as String[] is a single parameter.
Now here String[] can full fill the same purpose here but (String ...) provides more readability and easiness to use.
It also provides an option that we can pass multiple array of String rather than a single one using String[].
A feature of String[] vs String... is that the "String..." does not need to be part of the call.
public void test(String... args){
if(args.length > 0){
for( String text : args){
System.out.println(text);
}
}else{
System.out.println("No args defined.");
}
}
public void callerTest(){
test();
System.out.println();
test("tree");
System.out.println();
test("dog", "cat", "pigeon");
}
Then if you call callerTest(); the Output will be:
No args defined.
tree
dog
cat
pigeon
This seems to be always included in java's main method, but I still don't understand what it means honestly.
Edit: thanks!
In Java args contains the supplied command-line arguments as an array of String objects.
In other words, if you run your program in your terminal as :
C:/ java MyProgram one two
then args will contain ["one", "two"].
If you wanted to output the contents of args, you can just loop through them like this...
public class ArgumentExample {
public static void main(String[] args) {
for(int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}
}
}
The program will print in the terminal:
C:/ java MyProgram one two
one
two
C:/
Those are for command-line arguments in Java.
In other words, if you run
java MyProgram one two
Then args contains:
[ "one", "two" ]
public static void main(String [] args) {
String one = args[0]; //=="one"
String two = args[1]; //=="two"
}
The reason for this is to configure your application to run a particular way or provide it with some piece of information it needs.
If you are new to Java, I highly recommend reading through the official Oracle's Java™ Tutorials.
It's a String because the command line is expressed in text. If you want to convert that text into integers or booleans, you have to do that yourself - how would the operating system or Java bootstrapper know exactly how you wanted everything to be parsed? I suppose Java could look for a main method with the same number of parameters as command line arguments, and then try to parse them using Integer.parseInt etc... but this would be pretty complicated, and would still be inadequate in many situations.
As for it being mandatory - basically the Java designers decided to stick to a single method signature, which is frankly simpler than allowing for it to be optional. It would be possible though - in C#, you can have any of
static void Main()
static void Main(string[] args)
static int Main()
static int Main(string[] args)
Ultimately it doesn't make a lot of difference, to be honest. There's no significant downside in having to include the parameter.
The Java runtime system looks specifically for a method with a single String[] type parameter, because it wants to pass the parameters to your main method. If such a method is not present, it informs you through an exception.
If you want to treat the (string) command line parameters as integers or booleans, you are expected to do the conversion yourself. This allows you to handle the condition where somebody enters "ponies" where you expect an integer.
i have seen that both are used in java in the main. what is the difference?
Update: I just realized I never answered the question "What does “String[] args” contain in java?" :-) It's an array of the command-line arguments provided to the program, each argument being a String in the array.
And we now resume with our regularly-scheduled answer...
args is an array. To see individual command-line arguments, index into the array — args[0], args[1], etc.:
You can loop through the args like this:
public class Test
{
public static void main(String[] args)
{
int index;
for (index = 0; index < args.length; ++index)
{
System.out.println("args[" + index + "]: " + args[index]);
}
}
}
For java Test one two three, that will output:
args[0]: one args[1]: two args[2]: three
Or loop like this if you don't need the index:
public class Test
{
public static void main(String[] args)
{
for (String s : args)
{
System.out.println(s);
}
}
}
So, what does
"[Ljava.lang.String;@153c375"mean?
That's Java's default toString return value for String[] (an array of String). See Object#toString. The [ means "array", the L means "class or interface", and java.lang.String is self-explanatory. That part comes from Class#getName(). The ;@153c375 is ;@ followed by the hashCode of the array as a hex string. (I think the default implementation of hashCode for Object indicates where in memory the array is located, which is why it's different for different invocations of your program, but that's unspecified behavior and wouldn't be any use to you anyway.)
String[] args in main method is the String array of the command line arguments.
[Ljava.lang.String;@1d1e730 are the class name ([Ljava.lang.String is String[]) and the object's hashcode (@1d1e730);
if you want to print the actual values of the Strings in the array, you can use a simple for-each loop:
for(String arg:args)
System.out.println(arg);