Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

query for Java Exports

  Asked By: Kristopher    Date: Aug 02    Category: Java    Views: 536
  

I have a doubt regarding java's popular public static void main(String []
args) method.

The doubt is " What is the size of that string array args " ??

Please Clear me if anybody knows it.

Share: 

 

8 Answers Found

 
Answer #1    Answered By: Ludo Ricci     Answered On: Aug 02

The size  depends on the parameters passed in the
command line, you can get it (as with all the arrays)
with the length variable:

int size = args.length;

 
Answer #2    Answered By: Luigi Fischer     Answered On: Aug 02

it's right the length = args.length;

But some where the size  of the array  is defined. What's that size.

Is it 100,1000,10000 or dynamically assigned?

 
Answer #3    Answered By: Latasha Wilson     Answered On: Aug 02

I think the answer was rather obvious if you actually try it.

public class TestStuff{

public static  void main(String[] args){
for (int i = 0; i < args; i++){
System.out.println(args[i]);
}
}
}

Compile and run

java TestStuff then after put arguments in the command line to see what
happens

The String[] argus would essentialy equal

String[] args  = {"then", "after", "put", "arguments", "in", "the",
"command", "line", "to", "see", "what"};

Thus when you run TestStuff you would recive 11 printlns (because there
are 11 enteries in the string  array args).

So the answer is Dynamic.

 
Answer #4    Answered By: Ora Hanson     Answered On: Aug 02

the same program with different argument it will do amazing things, try this

C:\*.* (or) \\Network\Folder\*.java

pass these as arguments and see the results!!

public class TestStuff{

public static  void main(String[] args){
for (int i = 0; i < args; i++){
System.out.println(args[i]);
}
}
}

can anybody explain why this is happening??

 
Answer #5    Answered By: Angel Watkins     Answered On: Aug 02

the answers are:

1) Arrays in Java _can_ be assigned with a fixed size, but
nevertheless you can add as many new elements to them as you like to.
The only trouble you're causing this way (despite that it's a very
poor design if you do so, if you need a dynamic array  use the
ArrayList instead of an array) is that the built-in memory management
of Java has lots of unnecessary work to do; this slows down your
application considerably. But despite that there's no problem with
using an array and adding loads of new elements.
2) If you enter C:\*.* or something else that contains a
metacharacter like '*' or '?' (and all the other metacharacters used
by the various Unix shells), then the shell expands these strings
containing metacharacters to a list of all fitting file names, and
then this list of file names is passed to your Java class as
arguments to main(). Even the cmd.exe in Windows sometimes does this.
That's one reason why file names are usually enclosed in double
quotes "": to make sure that these strings are passed without file
name expansion to the respective program.

Everything clear  by now? If not, please ask again.

 
Answer #6    Answered By: Burkett Bernard     Answered On: Aug 02

Seems pretty obvious to me. The OS is enumerating all the files that match the
wildcards and passing all those filenames are arguments. Which is why it's a
good practice to use a "-" or "/" in front of command line options so that the
OS knows not to go searching for filenames.

 
Answer #7    Answered By: Perdita Lopez     Answered On: Aug 02

I'd hazzard a guess because \ is a special character.

 
Answer #8    Answered By: Faith Hughes     Answered On: Aug 02

Every array  in Java is dinamically assigned at runtime,
this is why the definition of an array does not include
the number of elements. The number of the elements is
included in the initialization part.

So, you can do something like that:
double[] x = new double [10];

Or with an known number at compile time:
int n;
double[] x;
...you can read n from a file or user input here...
x = new double [n];

Or make another variable point to this variable, so
the both variables will share the same memory space.
double[] y = x;

This is not like C, where you define a number of
elements at compile time, and obviously this number
has to be constant. Well, in C++ you can create
arrays dynamically similar to Java.

But in Java, once you have assigned n entries for
your array you cannot change the number of elements,
as Nico said you can use a container class for that,
like ArrayList or Vector.

 
Didn't find what you were looking for? Find more on query for Java Exports Or get search suggestion and latest updates.




Tagged: