#Basic Object Oriented Pointer Question

19 messages · Page 1 of 1 (latest)

mortal prawn
#

This is just a basic question. I don't really understand why in the context of this code the generic variable "Item" doesn't become a pointer shared to the same address the array points to.

The code:

public T pop(){
    T item = stack[size]; // stack is an array of type T
    stack[size] = null;
    return item;
}

(No exceptions are thrown- code compiles and runs successfully)

frail pendantBOT
#

⌛ This post has been reserved for your question.

Hey @mortal prawn! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

mortal prawn
#

for clarification, I thought this would happen:

#

and then when the array location is set to null I assumed null would be returned

#

This is what seems to actually happen:

stable blade
#

stack[size] = something doesn't modify the object contained in stack[size]
It puts another object (or here, the null pointer) in stack[size]

#

As usual

#

If you want to modify the object in stack[size] you need to do something of these sorts:

#

stack[size].stuff = thingy;

#

stack[size].incrementSomething();

inland fable
#

What really happens is you change what object an element of an array points to, but not move or delete it in any way! Arrays do not store objects themselves but only references to them

#

You expect item variable to be a pointer to a pointer to an object (T**), so, if you change to the original pointer, the other one will also point to null, but that's not true. Java variables can only store pointers directly to objects, therefore changing value of one of the pointers won't affect the rest.

frail pendantBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

frail pendantBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

mortal prawn
#

Thank you both!

frail pendantBOT
# mortal prawn Thank you both!

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.