Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why????

 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you should still be able to assign values to it, right?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you updated it to this, then it would work:
 
Herb Schildt
Author
Ranch Hand
Posts: 253
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhh......Got it. Ok, that made sense. Thanks, Thomas!!!
By the way, the result was 0.
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic