简体   繁体   中英

Where the Local object Will be Stored of String in java

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();
      }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM