-1

Well i am Referring Some basic Fundamentals of the java and as i come across to the Stack and Heap. as my understanding Heap is the main memory where the instance means object of the class will be stored and class Variable will be stored.

look about the stack storing all the method and the local variables and the method.

now my confusion is if i create any class object let say String object Locally into the method and its instance of the class String then where it can be stored. i did not got the proper conclusion for this can anybody suggest.

and i would apology if there if this could be duplicate of any other because i could not find similar so to clear my understanding i need help.

here the working example.

public class CreatingLcoalString {
      public void methodstring(){

          String s = new String("This is the area of confustion"); // this is the area of confustion
          System.out.println(s);
      }

      public static void main(String argsp[]){

          new CreatingLcoalString().methodstring();
      }
}
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • possible duplicate of [Where are instance's attributes in memory at runtime?](http://stackoverflow.com/questions/20992009/where-are-instances-attributes-in-memory-at-runtime) – Raedwald Sep 10 '14 at 06:52
  • Possible duplicate of http://stackoverflow.com/questions/23550385/where-are-instance-variables-of-an-object-stored-in-the-jvm – Raedwald Sep 10 '14 at 06:55

1 Answers1

2

The reference s will be stored on the stack. It will point to the String object on the heap. (ignoring escape analysis).

In your case 2 String objects will be created. One on heap and another in the String Constant pool (part of heap).

"This is the area of confustion" will be present in 2 places. Both are on the heap.

PS : Escape analysis can lead to the String being stored on the Stack. It is a special optimization mechanism introduced since jdk 6 update 23. More on it here and here

TheLostMind
  • 35,966
  • 12
  • 68
  • 104