• 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 ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

declaration vs definition

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic