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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
enthunoob
Posts: 57
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

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

Post by enthunoob »

¡latexpagebreak¿

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

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

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

Post 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

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

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

Post 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

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests