aspose file tools*
The moose likes Beginning Java and the fly likes define a array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "define a array" Watch "define a array" New topic
Author

define a array

sheng zhong
Greenhorn

Joined: Nov 23, 2006
Posts: 20
int N =12;
int near [] = new int[N];

Why should we use "= new int[N]" to define a array, what is the "new int[N]" means?

Why not define a array like this:
int near [12];
Pablo M�ller
Ranch Hand

Joined: Dec 12, 2006
Posts: 63
I don't know if this is exactly what you're asking, but in java arrays are trated like objects, even if they are of a primitive type.


2B || !2B
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by sheng zhong:
int N =12;
int near [] = new int[N];

Why should we use "= new int[N]" to define a array, what is the "new int[N]" means?

Why not define a array like this:
int near [12];

As Pablo mentioned, a Java array is an object. Its type includes the type of references (or primitive values) it will hold, along with its "dimensions" (for example, a one-dimensional array, two-dimensional array, three-dimensional array, etc.).

The array type does not include its size, which is required for the array's instantiation. (In this respect, you might think of the size as a parameter passed to the array's constructor.) So -- unlike dimensions -- the size of an array never appears in the declaration (left of the = assignment).

So in your example of int near [] = new int[N]; the declaration on the left-hand-side is a one-dimensional array of ints (the type). On the right-hand-side is the creation of a new object, which is an int array with a size of N.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
sheng zhong
Greenhorn

Joined: Nov 23, 2006
Posts: 20
Ok, I get it. I can't define a array in the way of C or c++.
Since it's an object, so we must assign a space for it. So it must have =new int[N], right?

Thank you~
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If you want to create and fill your array at the point of declaration, then you can use a shorthand syntax...

String[] myStrings = {"abc", "def", "ghi"};

Otherwise, creation of the array object is...

... = new type[size];
Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Hi

I think, by using the keyword new, this is an explicit memory allocation.

The statment String[] myStrings = {"abc", "def", "ghi"}; is an implicit memory allocation.

In both cases there is a memory allocation.

Regards
Urs


SCJP 1.4
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Urs Waefler:
...In both cases there is a memory allocation...

Yes, it's just a special shorthand syntax for arrays. The result is the same.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: define a array