Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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 ...
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Dynamic Memory Allocation for arrays

Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello I am Kavin Preethi , getting myself trained to learn the basics of Java

I have a doubt in the concept of dynamic memory allocation for arrays

Claim: Arrays are said to have dynamic memory allocation in Java
the array and declaration and initialization goes as follows :
String[] names= new String[3];
(In the above case, I am able to understand the new keyword denotes that the memory is being allocated dynamically)
Whereas consider the following syntax
String[] names={"xxx","yyy","zzz"};
(In the above case how do you still perceive that the memory is being allocated dynamically,this is my doubt)

Thanks in advance
Posts: 76121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where does that claim come from? It is useful to Quote Your Sources so everybody can read them.

When you declare an array variable, the JVM allocates space for the reference. In fact exactly the same happens for any kind of object. If it is a field, then the reference defaults to null and if it is a local variable, it might take whatever was in memory there before, so the compiler insists that reference be initialised before use.
That doesn't mean there are any objects associated, nor memory for the object. When you initialise the reference by creating an array object, the JVM finds enough memory for all the references. For new int[3] it needs 3 words for the ints and one word for the length parameter which is of course 3. Maybe more memory: don't know. Once that array is set up it is length 3, and you can't change the 3; you can however say

. . . = new int[6];

in which case the reference to the 3 array is lost and a new 6-member array is put in memory with 6 words for the ints, one word for the length (6) and maybe more memory: don't know.
Of course at this stage all the ints in your arrays are 0.

A bit more complicated for your String example; when the JVM loads the class, it sees the three Strings "xxx" "yyy" "zzz" and puts them into a special area in memory. A few microseconds later, when it executes {"xxx", "yyy", "zzz"} it finds the objects again and puts their references (memory locations) into the array, so the Strings can be found again. [This particular behaviour is peculiar to Strings, thought some other classes (eg Integer) show similar behaviour for some values.]

There are actually 3 ways to initialise an array:

new int[3];

That creates a 3-member array with default values (=0) for all the members.

{0, 1, 2, 3};

That is sort of syntactic sugar for "create an array of ints with the values 0, 1, 2, 3 in and count how many there are (4)." That syntax is only permitted in the same statement as

int[] numbers =

There is a hybrid which reads

new int[]{0, 1, 2, 3}

and can be used anywhere an array can be used. That means "create a new array of ints with the values 0, 1, 2, 3 in and count how many there are (4)."

String[] names = {"Kavin", "Preethi", "Narasimham"};

is like the 2nd example, it means "I want a String[] array and I want you to call it "names" and I want these values in it: "Kavin", "Preethi", "Narasimham", and I want you to count how many there are."

* * * * * *
In Java5 and 6 you can also create arrays with varargs, but I'll say no more about that here.
    Bookmark Topic Watch Topic
  • New Topic