Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > int[] x = new int[...]

int[] x = new int[...]
Thread Tools
Dedicated MacNNer
Join Date: Nov 2000
Location: Netherlands
Status: Offline
Reply With Quote
Oct 18, 2002, 07:05 AM
 
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
Reply With Quote
Oct 18, 2002, 10:39 AM
 
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
Reply With Quote
Oct 18, 2002, 01:42 PM
 
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
     
JNI
Forum Regular
Join Date: Oct 2002
Location: Left Coast
Status: Offline
Reply With Quote
Oct 19, 2002, 04:28 AM
 
> 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.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:13 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2