Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

declaration vs definition

 
Nithya Shiva
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic