When we create a object of a classtype the new operator allocates the memory at the run time.
Say
myclass obj1 = new myclass();
here the myclass()
defines a constructur of myclass
but
int arr1[4] = new int[];
new allocates the memory b开发者_如何学运维ut, what the int[]
does here?
New allocates the memory but, what the int[] does here?
The int[]
part simply specifies what type the newly allocated memory should have.
However, since an array can't grow dynamically, it doesn't make sense to write new int[]
(even though you've specified int[4]
in the declaration), you'll have to write it either like this
int arr1[] = new int[4];
or like this
int arr1[] = {1, 2, 3, 4};
Relevant sections in the JLS:
- 15.10 Array Creation Expressions
- 10.6 Array Initializers
Your code:
int arr1[4] = new int[];
will not compile. It should be:
int arr1[] = new int[4];
putting []
before the array name is considered good practice, so you should do:
int[] arr1 = new int[4];
in general an array is created as:
type[] arrayName = new type[size];
The [size]
part above specifies the size of the array to be allocated.
And why do we use
new
while creating an array?
Because arrays in Java are objects. The name of the array arrayName
in above example is not the actual array, but just a reference. The new
operator creates the array on the heap and returns the reference to the newly created array object which is then assigned to arrayName
.
The above program declares anArray with the following line of code:
int[] anArray;
// declares an array of integers
Like declarations for variables of other types, an array declaration has two components: the array's type and the array's name. An array's type is written as type[], where type is the data type of the contained elements; the square brackets are special symbols indicating that this variable holds an array. The size of the array is not part of its type (which is why the brackets are empty). As with variables of other types, the declaration does not actually create an array — it simply tells the compiler that this variable will hold an array of the specified type.
Creates an instance of the object. new
allocates memory on heap. the int arr1[4]=new int[];
allocates 14 bytes on heap.
The correct declaration of int array is like following:
int[] arr = new int[] {1,2,3}; // legal, array of size 3
int[] a = new int[100]; // Declare and allocate, array of size 100
int[] a=new int[5];
- In the above array declaration int[] is the array type i.e integer type array
a
is the array name for example to declare a variable 'a'------> int a; same way for arrays int[] a where [] is to indicate declaring array only diff is []
now come to new
new is an operator what is function of this operator?
this new operator goes to heap memory and allocate an array (due to int[])and allocate 5 values inside array type int(due to int[5]) calls the reference variable.here reference variable is arrayname i.e a.
whatever array created in heap is given to reference variable a
memory diagram
---------------------
| in heap memory |
| |
||------------------ |
|| | | | | | |
|------------------- |
/ ---------------------
/
/
a/
5 positions as int[5]
in above example and reference variable point to array memory location
精彩评论