dcsimg
Working with arrays
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   D_Chatterjee
Posted On:   Tuesday, July 15, 2003 10:20 PM

Hi, I have a confusion regarding the use of 'new' operator in arrays. I have created an array of object with length as 5, using the 'new'operator.This means the complier reserved 5 memory location for holding the elements of the array.But when i initialize the array, i am again using a 'new' operator to create the object. My question is , if i am using 'new' operator again then does the complier allocates a new memory loaction for the object, or it uses the same memory reserved by the array. For convenience i have pasted the source code: ------------------------------------------------------------ //This is the first class public class ArrayExample { MyArray[] myArray; public ArrayExa   More>>

Hi,
I have a confusion regarding the use of 'new' operator in arrays.
I have created an array of object with length as 5, using the 'new'operator.This means the complier reserved 5 memory location for holding the elements of the array.But when i initialize the array, i am again using a 'new' operator to create the object. My question is , if i am using 'new' operator again then does the complier allocates a new memory loaction for the object, or it uses the same memory reserved by the array.
For convenience i have pasted the source code:
------------------------------------------------------------
//This is the first class

public class ArrayExample
{

MyArray[] myArray;
public ArrayExample (int lth)
{

myArray = new MyArray [lth];
System.out.println("This is an array of length "+myArray.length+"
");
for (int i = 0; i
{

myArray[i] = new MyArray ();
System.out.println("The name of the array is "+myArray[i]+"
");
myArray[i].testMethod ();
}
}

public static void main (String args[])
{
ArrayExample arrayExample = new ArrayExample (5);
}

}
------------------------------------------------------------
//This is the second class

public class MyArray
{

public MyArray ()
{

}

public void testMethod ()
{
System.out.println("This is object in the array");
}
}
------------------------------------------------------------

   <<Less

Re: Working with arrays

Posted By:   Anonymous  
Posted On:   Wednesday, July 16, 2003 10:16 PM

Hi Chatterjee,

MyArray [] myArray = new MyArray [lth]

An Array object would be created which can have references to the MyArray objects.
MyArray arrElem = new MyArray()

A MyArray object owuld be created.

Hope this is clear...

Re: Working with arrays

Posted By:   Ivo_Limmen  
Posted On:   Tuesday, July 15, 2003 11:19 PM

The compiler reserves memory when you use the keyword new. But you also use it to create a new object with this keyword.
Look at the next example:

int[] array = new int[2];
int[0] = 1;
int[1] = 2;

As you see, I only use new to reserve the memory for 2 integers. If you use objects (like String, ect.) you will need to use the new statement again to create the object in that memory slot.
I hope this explains it a bit more...
About | Sitemap | Contact