 |
 |
int[] x = new int[...]
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 2000
Location: Netherlands
Status:
Offline
|
|
I'm just starting with programming in Java, and I'm a little confused about this. If you declare an array you do it like:
int[] x = new int[100];
But isn't this a bit overdone? This question may sound dumb to you all, and it's not really critical to my understanding of the language, and probably this is just the way it is, but still I'm a little confused as to why you have to say int[] twice.
|
|
"Chance is irrelevant. We will succeed."
== 7 of 9 ==
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 1999
Location: Ottawa, ON, Canada
Status:
Offline
|
|
The first tells the compiler "this will be an array" - the second tells how big. Yeah, one could probably have the compiler figure it out from the second bit, but it's easier this way.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
If not, then you could do this:
int x;
x = new int[100];
But that would be bad, as x is an integer, and you're trying to set it equal to an array of integers. It's all about typing.
Welcome to Java, where I began my adventure into programming not too long ago...
F-bacher
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2002
Location: Left Coast
Status:
Offline
|
|
> int[] x = new int[100];
> But isn't this a bit overdone?
[...]
> but still I'm a little confused as to
> why you have to say int[] twice.
I guess it would be possible for the compiler to guess if you were able to write something like:
int[] x = new [100];
The compiler could assume you meant you wanted to allocate an array of ints. But Java is pretty strict on its typeing and it doesn't want to guess too much. It wants to make sure you wrote what you really meant.
If those were Classes instead of primitives you might want to do something like this:
Object[] a = new Cat[13];
Here you have an abstract variable x assigned to an array of a specific class type. So it isn't always the case the the R value is of the same type as the L value.
A bit more info (since you say you are just starting out, this may help clarify why you do it that way.
First off, Java comes from C/C++, and that is essentially the way those languages do it.
What you are doing is really two different actions. The L Value (left of the = sign) is a declaration of a variable named x. The R Value (right of the = sign) is an allocation of an array. You have to think of them as separate actions (called sequence points). Your example is just one of many possible code snippets that can occur in the language, and the language must work for other cases also. E.g. if you were doing something like this you can see why the two things are needed distinctly:
void foo(int[] a, int b) {
int[] x;
int y;
x = a;
y = b;
}
In this case, some other code has done the allocation and is passing it in as an argument. So you won't always be declaring and allocating on the same line of code as in your example.
Hope this helps.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |