Page 1 of 1

[HD Pg 225, Sec. 8.7.2 - passing-objects-to-methods]

Posted: Fri Jul 23, 2021 6:13 am
by enthunoob
¡latexpagebreak¿

This thing is twice in this chapter, I suppose it's not intentional (I'm reading version 28.0)

Re: [HD Pg 225, Sec. 8.7.2 - passing-objects-to-methods]

Posted: Sun Jun 16, 2024 9:37 am
by raphaelzintec
hello

i don't understand the image with heap space on the page 224 from the pdf version 2019

it shows 1 stack space for main method and 1 stack space for modifyData method

but this modifyData method cannot have it's own stack, since it uses the main stack after main have called it.

only a thread can have it's own stack ... not a method..?!

plz tell me what happens here:

Code: Select all

public class App {
    public static void main(String[] args) {
        m();
    }

    public static void m(){
        System.out.println("hello");
    }
}
1. main and m() will have 2 different stacks in this program
2. main and m() will share the same stack
3. m() will have a dedicated frame in the main stack (chatgpt told me this cuz i dont remember having seen something about frames before in your pdf, and chatgpt sometimes tell me that each method has it's own stack then he says the opposite... so i'm confused)

thanks

Re: [HD Pg 225, Sec. 8.7.2 - passing-objects-to-methods]

Posted: Sun Jun 16, 2024 8:42 pm
by admin
It is true that threads (not methods) have a stack (as explained in chapter 1.6 Stack and Heap). When a method is invoked from a thread, the space that that the method uses on that stack is called a "frame". "Stack space" is just some space on a stack.

But you are right. In the given example, both the methods will share the same stack (because they are invoked from the same thread) but their frames will be different. Thus, the spaces that the two methods use on the stack will be different. The diagram should say stack frame instead of space to avoid this confusion. But technically, it is correct because it is just referring to two different spaces on a stack. Since it does not show the space occupied by the whole stack, it looks as if these are two different stacks. This should be improved.

Re: [HD Pg 225, Sec. 8.7.2 - passing-objects-to-methods]

Posted: Sun Jun 16, 2024 9:53 pm
by raphaelzintec
tnx a lot it's very clear now i feel much better understanding this stack & heap cuz it's important for job interviews