Understanding the three steps of object declaration, creation, and assignment (easy)
I am trying to understand the three steps of object declaration, creation and assignment, and want to know if what I wrote about the picture is correct (from Head First Java, but edited text below to use my own words and see if I can explain it properly):
1) Declare a reference variable
Dog myDog
Here we tell JVM to make space for a reference variable. This reference can only EVER be a type Dog
from now on.
myDog
is the name we give the reference variable, to make it more readable and usable for us.
Beneath myDog
is actually a bunch of bits representing a way to get to the actual object on the heap (e.g. @Dog1234
fx). NOT SURE if this is true
2) Create an object
new Dog();
Here we tell JVM to make space for a new Object instance on the Heap. Its the actual Dog
object that is stored somewhere in the heap by a certain address (e.g. @Dog1234
).
3) Link the object and reference
=
Last part is to assign the new Dog()
we created on the heap to the reference variable myDog
. In other words, we make sure that the reference variable we call myDog
is connected to the actual new Dog()
we created, and therefore are able to control the object on the heap through this reference variable myDog
.21 Replies
⌛
This post has been reserved for your question.
Hey @Steadhaven! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose 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.
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
This is also a thing I want to ask about
sometimes I see something like
List<String> animals = new ArrayList<>();
(or something like that, if my syntax is wrong)
I don't understand what it exactly means.
Is the left side more important than the right? Is it a List, or is it an ArrayList?
I heard a guy say something like "Oh we don't need to write ArrayList<String> animals = new ArrayList<>();
but just List<String> animals = new ArrayList<>();
since we don't need something from the ArrayList...
Something like that. I don't understand this topic at all.Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
Yea apparently @Dog1234 is just the toString sorry
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
Ok thanks
We cannot use any method/field that is exclusive to either
ArrayList
or LinkedList
in the above example.
However, the benefit has to be that whatever method/field they have in common with List
itself, will be different in their implementation because they can override it.
So by changing the instantiation from ArrayList
to LinkedList
on the Right Hand Side, we can still only use the same methods/fields as before, but now they might behave a bit differently.
Is this correct? Or does it not change anything?Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
So then later writing
animals = new LinkedList<>();
does practically nothing?Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
Aha, so the thing that confuses me here, is that I don't see what the difference is between:
Since its the left hand side that decides the type, and we said we want the behavior of a list so we made our type List.
What difference then does the right hand side do?
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
yea but they are identitical right? SInce I said both are type
List
on the left hand side
there should be no difference
in their speed, or the method/fields available
since its the left that decided all that
they are both type List
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
No sorry, I miswrote
only speaking about these two
they seem identical, since both are declared as
List
type on left hand side
they are two distinct List of course, but I mean their type is identical and therefore they have similar kind of speed/big O, and method/field available
regardless of the right side (new ArrayList
or new LinkedList
)Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
My confusion is just what the difference is between the left hand side type and the right hand side type.
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
Ok thanks for the clarification. Hopefully I finally understood the difference.
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.
Post Closed
This post has been closed by <@305362010374406144>.