This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Why???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why????" Watch "Why????" New topic
Author

Why????

Shannon Sims
Ranch Hand

Joined: Jul 03, 2003
Posts: 197
Can someone please explain why I can't assign a value to args??? Below is the snip of code...
public static void main(String[] args)
{
int i=2;
args[0] = new String("two"); //why can't this be done?
args[1] = new String("three"); //why can't this be done?
boolean b = true;
System.out.println( "Values are:"+( b != b )+","+( i = args.length )+","+( b=i==2 ) );
}
Help is greatly appreciated!
Carl Pettersson
Ranch Hand

Joined: Sep 09, 2003
Posts: 73
args' size is set by the number of arguments you give when you start your program, so it would be size 0 if you don't start it with any arguments.
I think
Shannon Sims
Ranch Hand

Joined: Jul 03, 2003
Posts: 197
But you should still be able to assign values to it, right?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
If you updated it to this, then it would work:


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
Shannon:
Carl's answer is correct. (As is Thomas' alternative.) However, even if you executed your program with sufficient command-line arguments to enable the assignments that you show, not all programmers would agree that doing so is good practice. What is it that you are trying to accomplish?
[ October 21, 2003: Message edited by: Herb Schildt ]

For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Shannon Sims:
But you should still be able to assign values to it, right?

No. Imagine I create an array like this:
int[] array = new int[5];
How many items can I put in this array? Can I put in 5 items? 10? 1 million? What happens if I do this array[10] = 0;?
I am limited to the size of the array that I created so the answer is 5. Attempting to access the 10th entry will cause my program to throw an exception.
The array that comes in to your program containing your arguments also has a size. That size is determined by the number of arguments that are passed in. If no arguments are passed in then the array has a size of zero and no items can be stored in it.
Shannon Sims
Ranch Hand

Joined: Jul 03, 2003
Posts: 197
Thomas, your explanation made sense, but then I tried your first post and it worked:
public static void main(String[] args)
{
int i=2;
args = new String[2];
args[0] = new String("two");
args[1] = new String("three");
boolean b = true;
System.out.println( "Values are:"+( b != b )+","+( i = args.length )+","+( b=i==2 ) );
}
Code compiled and printed this:
Values are:false,2,true
So, I guess you can change the size of args. The purpose of this code was to test what would happen with ( b = i == 2 ). So I needed to assign values to args since I'm not sure how you can get input from System.in using Visual Age.
Thanks for all the help!
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Shannon Sims:
So, I guess you can change the size of args.
You can not change the size of args. You can create a brand new args of a different size. Try this:
int[] array = new int[1];
array[0] = 5;
array = new int[5];
System.out.println(array[0]);
Guess what prints...
Shannon Sims
Ranch Hand

Joined: Jul 03, 2003
Posts: 197
Ahhh......Got it. Ok, that made sense. Thanks, Thomas!!!
By the way, the result was 0.
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Thomas, is it because you created a new array that the result was 0?
Lemme see if I get this?
You instantiated an array of 2 values (integers) 0-1.
You then assigned the first value of the array to 5.
You then created a new array(extended) of 5 values (integers) 0-4.
the output was 0 due to the new/extended array?
Thanks for clearing this up if you do.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Steve
Yes the answer is Zero because of the new array. A new array would be a better term than extended array because the array being created is totally new and doesn't contains anything(empty), While the term extension refers to addition of elements in the array.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Steve Wysocki:
Thomas, is it because you created a new array that the result was 0?
Lemme see if I get this?
You instantiated an array of 2 values (integers) 0-1.

You go it... except that "new int[1]" creates an array of 1 value. The number you specify in the "new" is always the size.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why????
 
Similar Threads
What can i do and don't with this collection declaration ?
Scanner and MatchResult
The difference of String and StringBuffer?
Garbage collection can get complicated
I am a real JAVA beginner, please take a look at this code.