Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

define a array

 
sheng zhong
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sheng zhong
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic