This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
Nithya Shiva wrote:Can someone please explain when memory will be allocated in java?
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 ?
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.