• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Need info on memory allocation...

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I will post the code and then make my question...

public class SampleClass{
String s1;
int a;
public static void main(String args[]){
String s2 = null;
//Some set of code where objects (including object of SampleClass) get created
}
}

In the above code, does memory get allocated for variables "s1" and "a" before any instance is created?
Even if instance is created, will the memory be created for the string and int before assigning some values to them?

Regards,
Sriram
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sriram Sharma wrote:In the above code, does memory get allocated for variables "s1" and "a" before any instance is created?
Even if instance is created, will the memory be created for the string and int before assigning some values to them?


No and no.

Well, for the second part: there will be some memory allocated to store the variables, but the String variable will be initialized to null - there is no memory allocated for a String object.
 
Sriram Sharma
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick reply Mr.Young!!!
So, I have 2 questions now...

1. The variable table or the storing case for variables will be on heap or in stack?
Basically, I wanna know where in memory will the variables be stored.
2. When we say "null", where is this null stored? I understand that null is a default value for string and objects, but this has to be pointed at some place in the memory... right?
Or is it a runtime decision that the JVM will take the value for variable as null since there is no value assigned to it?

Please clarify.

Regards,
Sriraam
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Objects live on the heap, so the memory space for the instance variables of the object is on the heap.

2. "null" is only a special indicator that says "this variable refers to no object". There is no place in memory where "null" is stored.
 
Sriram Sharma
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Young!!!
You made it clear :-)

Regards,
Sriram
 
Sriram Sharma
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Young,

Now, I get one more question on this front....
When we say "String S1;", the compiler will have atleast some place for the S1 variable to be stored... right???
Once the compiling is done and when the code starts running, the JVM should remember that S1 is a variable of type String and for this purpose some memory should be allocated.
If that is the case, then, will this variable be stored in stack or heap?

Regards,
Sriram
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

As per my understanding , the reference variable s1 of type String will be created and stored in stack.
After initialization this reference variable will be made to point to a string object in Heap or to a string object in string literal pool. (Based on the initialization).

Jesper please correct me if am wrong.

Thanks,
VedhaVishali.
 
Sriram Sharma
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vedha,

Thanks for the clarification! :-)
By the way, One small correction....
As far as I know, String objects get created only in heap and not in the literal pool.
In String literal pool only a reference to the String object is stored.
Irrespective of the type of creation, String object will be created in heap only.
Anybody...Correct me if wrong!

Regards,
Sriram
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vedha Vishali wrote:As per my understanding , the reference variable s1 of type String will be created and stored in stack.


No, only local variables (inside methods) are created on the stack. The variable s1 is an instance variable, not a local variable - the memory space for variable s1 is part of the memory space for a SampleClass object (on the heap).

Sriram: You're right - objects are always on the heap, and the String pool just refers to String objects that are on the heap.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic