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.
No description
21 Replies
JavaBot
JavaBot9mo ago
This post has been reserved for your question.
Hey @Steadhaven! 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.
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Steadhaven
SteadhavenOP9mo ago
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
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Steadhaven
SteadhavenOP9mo ago
Yea apparently @Dog1234 is just the toString sorry
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Steadhaven
SteadhavenOP9mo ago
Ok thanks
List<String> animals = new ArrayList<>();
// ^type List
animals = new LinkedList<>(); //not sure this is how you create LinkedList
// ^still type List
List<String> animals = new ArrayList<>();
// ^type List
animals = new LinkedList<>(); //not sure this is how you create LinkedList
// ^still type List
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
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Steadhaven
SteadhavenOP9mo ago
So then later writing animals = new LinkedList<>(); does practically nothing?
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Steadhaven
SteadhavenOP9mo ago
Aha, so the thing that confuses me here, is that I don't see what the difference is between:
//difference between
List<String> animals1 = new ArrayList<>();
List<String> animals2 = new LinkedList<>();

//simarly does this even do anything practically new:
List<String> animals = new ArrayList<>();
animals = new LinkedList<>();
//difference between
List<String> animals1 = new ArrayList<>();
List<String> animals2 = new LinkedList<>();

//simarly does this even do anything practically new:
List<String> animals = new ArrayList<>();
animals = new LinkedList<>();
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
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Steadhaven
SteadhavenOP9mo ago
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
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Steadhaven
SteadhavenOP9mo ago
No sorry, I miswrote
//difference between
List<String> animals1 = new ArrayList<>();
List<String> animals2 = new LinkedList<>();
//difference between
List<String> animals1 = new ArrayList<>();
List<String> animals2 = new LinkedList<>();
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
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Steadhaven
SteadhavenOP9mo ago
My confusion is just what the difference is between the left hand side type and the right hand side type.
Car myCar1 = new Lamborghini();
Lamborghini myCar2 = new Lamborghini();

Car myCar3 = new Prius();
Prius myCar4 = new Prius();

Car myCar5 = new Lamborghini();
myCar5 = new Prius();

Car myCar6 = new Prius();
myCar6 = new Lamborghini();
Car myCar1 = new Lamborghini();
Lamborghini myCar2 = new Lamborghini();

Car myCar3 = new Prius();
Prius myCar4 = new Prius();

Car myCar5 = new Lamborghini();
myCar5 = new Prius();

Car myCar6 = new Prius();
myCar6 = new Lamborghini();
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Steadhaven
SteadhavenOP9mo ago
Ok thanks for the clarification. Hopefully I finally understood the difference.
JavaBot
JavaBot9mo ago
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.
JavaBot
JavaBot9mo ago
Post Closed
This post has been closed by <@305362010374406144>.

Did you find this page helpful?