| Author |
declaration vs definition
|
Nithya Shiva
Greenhorn
Joined: Sep 02, 2009
Posts: 1
|
|
Can someone please explain when memory will be allocated in java?
Ex:
Classname c; // just tells compiler abt name and type
c = new Classname () //allocates
how about primitive types?
int i; // does this allocate memory and initialize with deafult value for ints ?
Thanks
-Nithya
|
 |
Suren Singh
Greenhorn
Joined: Aug 03, 2009
Posts: 15
|
|
Welcome to Java Ranch
Memory allocation would only happen at run time . It doesnot depend on whether a type is primitive or class or any other custom type .
Thanks
Suren
|
 |
R van Vliet
Ranch Hand
Joined: Nov 10, 2007
Posts: 144
|
|
Nithya Shiva wrote:Can someone please explain when memory will be allocated in java?
Ex:
Classname c; // just tells compiler abt name and type
c = new Classname () //allocates
how about primitive types?
int i; // does this allocate memory and initialize with deafult value for ints ?
Thanks
-Nithya
All memory allocation happens during runtime obviously but I'm assuming that wasn't your question. In the case of primitive types memory for that value is allocated when the code reaches "int i" at which point it is initialized with the default value for that primitive type. By the way, the "Classname c" declaration isn't free either since the memory needed to store the reference to a future but at that point non existent instance takes up some memory as well.
|
 |
 |
|
|
subject: declaration vs definition
|
|
|