• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

define a array

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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];
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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];
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic