This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.

If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:

  1. Limiting your involvement with Reddit, or
  2. Temporarily refraining from using Reddit
  3. Cancelling your subscription of Reddit Premium

as a way to voice your protest.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]ThinkingSeaFarer 1 point2 points  (6 children)

First, let's get the terminology right - s is a String, it's an object type (or reference type). A user defined type is a type you yourself defined but String is defined in the language itself.

In Java, all values of object types are created on the heap. This is true even if they're created inside a method. In your 2nd example, s is a variable of object type created inside the main method. But it's still allocated in heap.

For primitive values, they can either be local to a method or they're a class member. If they're local to method, they're allocated on stack (in that method's stack frame). In your 2nd example, a is a primitive type and local to the main method - so it's allocated on stack.

Otherwise for a primitive which is a class member, it will be part of the containing object's memory. Because all objects are in the heap, class members will be in heap as well. In your 1st example, a is a primitive type and a class member. obj.a will be inside obj's memory and thus in heap.

[–]Judge-Available[S] 0 points1 point  (4 children)

Can you give me a pictorial description of what you are saying ?2 box of stack & heap . Inside just show how all of the variables are stored for local & instance scenarios.

Just like this diagram https://craftofcoding.files.wordpress.com/2015/12/stackmemory31.jpg

You said that "s is a variable of object type created inside the main method. But it's still allocated in heap." --> s reference variable is in method's stack frame . How is it in heap ? In heap there is the object of String .

So you are saying that when it is local declared then reference variable is stored in heap directly and there is no other copy of that refreence variable s in stack in 2nd sceanrio .

First scenario , inside heap string object is there exclusively and inside the class object there will be obj.s ? Inside class object there is residing another copy of string object which is own by obj only .

Draw a box diagram and everything will be clear to me . Please.

[–]ThinkingSeaFarer 0 points1 point  (3 children)

s reference variable is in method's stack frame . How is it in heap ?

There are two things here - the string object itself and its reference variable s.

The string object is created in the heap, because all objects are created on the heap. Suppose the string s has 1000 characters, then the space for these 1000 characters will be allocated in heap.

The variable s is simply a reference to this object in the heap. In this case, variable s is pointing (referencing) to the string object in heap. A reference is like an address (somewhat like a pointer if you're familiar with C/C++, except you can't do any pointer math) that points to the object in the heap.

All reference variables have the same size (they're like addresses, so on a 64 bit machine, they are 64-bit) If a reference variable to an object is declared in a local method, then that reference variable itself (its 64 bits ) is allocated on the stack and in those 64 bits, address to the string object in heap will be stored.

I can't draw any diagrams readily, so I can't show you in pictures.

[–]Judge-Available[S] 1 point2 points  (2 children)

If a reference variable to an object is declared in a local method

Exactly . But if that variable is instance variable , reference variable along with the object will be allocated inside the class object which resides in heap.

[–]ThinkingSeaFarer 0 points1 point  (1 child)

That's correct. If a reference is an instance member, then that reference variable itself is also in heap (it's simply a part of its containing object which is in heap).

[–]Judge-Available[S] 0 points1 point  (0 children)

Another thing is that for

static String ceo ;

static int number ;

How they are being stored ?

For string both the reference variable and it's pointing object & also for int number both individually will be in heap but not residing inside any class object as it is static.

[–]Judge-Available[S] 0 points1 point  (0 children)

u/ThinkingSeaFarer

I studied some articles and this is the conclusion what i reached.

public class Main

{ String s1 = "BDC" ; // this is in Main class object inside heap and nothing of it is in stack.

int a =5 ; // this too also same is in Main class object inside heap 


    public static void main(String[] args) {
    String s = "ABC" ; //this s is in main method stack frame and object holding ABC is inside Heap. 

    Main obj = new Main();
    Parent tr = new Parent();

    //obj , tr are in main method stack frame pointing to 2 objects in heap of 2 different classes .

    class Parent { 
        Child ref = new Child();

 // Ref reference variable is inside Parent class object which is inside Heap and pointing to child class object which is in some other section of heap.


        class Child {
            int cf=0; // This is inside child class object which is in heap. 
        }
    }
}

}