#Understanding the three steps of object declaration, creation, and assignment (easy)

49 messages · Page 1 of 1 (latest)

proud kite
#

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.

atomic quarryBOT
#

This post has been reserved for your question.

Hey @proud kite! 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.

nova tendon
#

new XXX is referred to as an instantiation or initialisation not "create" ...when u make an instance of an Object, you are instantiating an object

= is refered to as an assignment statement, not a "link"

#
//Declaration
Dog myDog;

//Instantiation
new Dog;

//Assignment
myDog = new Dog();

all 3 above statements are valid java as they stand,

but the instantiation doesnt make sense, because the onstance is immediately lost so its mmediately destroyed

an instantiation will always include an assignment

myDog = new Dog();

but there is nothing wrong with also including a declaration

#

also:

This reference can only EVER be a type Dog
this is completely wrong

it can be any sub-type of dog

String myString = "Hello World";
Object myObject = myString;

all Objects are sub-types of Objects so u can put anything into an Object type, even a String

#

u dont need to understand about sub-types or inheritence yet, but its not true that u can only put the identical type into a referance variable

proud kite
#

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.

nova tendon
#

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
Also not true

the default toString of an object does include its heap address (because if u dont tell java how to compare instances of a custom class of urs the only way java can tell is by using their address)
....BUT u cant use that value to access the object because java doesnt allow u to modify/manipulate referances

proud kite
nova tendon
#

i'll b back in 20mins, will explain more then

proud kite
#
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?

nova tendon
#

sort of correct

#

List is an interface not a class

#
public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable

this is the class declaration for ArrayList

#

it actually implements FOUR seperate interfaces........

#

...
an interface doesnt define what a class is, it defines a way for it to behave

and in the case of:

List<String> animals = new ArrayList<>();

u want the behaviour of a list so u make ur type List

proud kite
nova tendon
#

and, yes by declaring it that way, it does mean that u can swap out differant implementations

nova tendon
#

but all of ur code will still work

proud kite
# nova tendon oh it does a lot, LinkedLists suck using modern hardware

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<>();

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?

nova tendon
#

well, in the similarly block, u only have 1 variable, so the ArrayList disappears .... u can never call it, never use it and all data it used to hold is not gone

#

in differance code lock u have 2 variables, so u can call either of them whenever u like to get their data or add more data, without any trouble

proud kite
#

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

nova tendon
#

how is having TWO lists vs having ONE list identical??

proud kite
proud kite
#

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)

nova tendon
#

i mean .... there is no difference between a Lamborghini and a Prius right?

they are both cars, so their speed should be the same
as long as u call them both cars they will perform the same right?

#

they both have 4 wheels what more do u need?

nova tendon
proud kite
nova tendon
#

the left hand side type is an implementation (it has real code and it can make instances)
the type on the right is a concept of behaviour (it tells java what ur expecting to do with the instances)

just try writing the code:

List<String> myList = new List<>();

and see what java tells u

#

the point is u want a list, so u tell java 'im going to use a list'

but there is not actually a thing as a list ...... because a "list" is a description of a bunch of the same things placed in order where u can add more, delete if u want, etc
so u have to give it a thing that has followed the RULES that List described

#

just like i tell u to give me a table

u will ask "what table?
do u want a wood table, plastic table, glass table?
do u want 4 legs, 3 legs, 6 legs?
do u want a dinner table, coffee table, bedside table?
do u legs on the outside, legs in the center?"

a table is an idea
and there are MANY DIFFERANT ways to make a table

#

...
ArrayList, LinkedList, PriorityList, RecentlyUsedList, UniqueList (no duplicates), SkipList, ConcurrentList, ImmutableList, ObservableList

...and those are just the kinds i know of that i can remember in a short time

#

each one is differant, each one has differant goals and differant specialities ...... but every single one is a list
every single one has a add() method and a get(index) method and a includes(object) method

proud kite
atomic quarryBOT