Now, I know you're just itching
to build an array-based list,
and we will soon.
But before we do that,
just for the sake
of really good scholarship,
let's compare and contrast
arrays and classes.
So when it comes to
implementing a data structure,
say a list or a set or a map
or any other number of things,
both arrays and classes
can be used to organize
a bunch of memory boxes.
Now, they have
different flavors.
So for example,
the planet class,
we have a bunch of named fields.
And for example,
here I've constructed
a planet earth that has a
string name and a double mass.
By contrast, arrays, as we've
noted, are a set of numbered,
or a sequence of
numbered, memory boxes.
Now, for arrays, they have
to be all the same type.
And if we want to access
them, we use bracket notation.
And classes, they could
be all different types.
And we access them
using dot notation.
In both cases, though, we have
a fixed number of memory boxes.
That can never change, at
least in the Java language.
Other languages may
be more permissive.
So let's imagine what's
really different about that.
What, philosophically,
is really important here?
Well, one really big difference
is that with arrays, we
can actually compute the
indices we want at runtime.
So imagine the following.
We construct an
integer array, like so,
and then we ask our user,
what index do you want?
So we try running this program,
which I'm not going to do live,
but it would say, what
index do you want?
And we compile and
run this program,
and maybe we type in two.
So now we have this
index of interest, two,
that was not known
at compile time,
and then we can say, hey,
give me back item number
two from this array.
And when we print it
out, we'll get 102.
By contrast, with classes,
we cannot compute those
at runtime.
So for example, suppose we
have a field of interest, mass,
and then we actually try to get
back that field of interest.
So in this case, rather
than asking the user,
I'm just having the
string hard-coded in here
into this field.
And in this case,
what happens when
we try to do earth
fieldOfInterest,
the compiler says no.
You cannot say earth
fieldOfInterest.
That is forbidden.
The reason is that's
just how Java works.
You cannot throw in a particular
field you want and get it back.
That's just how the
language is designed.
It will say array required,
but instead a planet was found.
Now, you might try and say,
I'm going to be extra clever.
What I'm going to
do is I'm going
to use dot notation, because
I know that's what's supposed
to happen with classes.
But in this case, when I
say earth.fieldOfInterest,
then I will get, I don't
know what you mean.
I tried to find something
called fieldOfInterest
that's part of the planet class,
but as you note, I have mass.
I have name.
I do not have fieldOfInterest.
So in Java, you
just can't do that.
Now, I'm kind of lying,
because there is technically
a way of doing that.
And if you want, you
can click this link
and learn all about something
called the reflections API,
but it's not something
that's considered good style.
So ultimately, in Java,
when you're using a class,
the only easy way to
access a member of a class
is with what I'll call
hard-coded dot notation.
So for example, if I
want to get back mass,
I can only do it
by saying p.mass.
I can't do p.fieldOfInterest
or p braces fieldOfInterest.
It won't work.
And so the Java
compiler-- ultimately,
the idea here is it
doesn't treat something
on either side of a
dot as an expression.
It won't actually
evaluate this thing.
And thus it won't work.
And if you're curious
about these kind
of philosophical issues,
like if you're kind of--
if you're the kind of person
who looks at this and says,
there's something deep
here and something cool,
I encourage you to later take
a compilers or a programming
languages course,
which will really
go into the philosophy of
these things in more depth.
Now, it is possible--
I'll just make a
quick note, though--
you can do funny things
if you really want.
And the reason that
Java doesn't let you
do this, just to mention
this philosophically,
is that it keeps
our code simple.
Reflection leads to
ugly code, strange code.
If Java actually
let you do this,
it would cause a
lot of headaches.
And so the Java
designer said no,
and they decided, well,
if you really want,
you can use the reflections API.
But everybody knows--
I know, you now know--
that using reflection
for this kind of purpose
is just considered
terrible style.
And the reason is simply
that with big programs,
it's just an extra thing
you have to think about.
So the language was
built to restrict
the choices you
could make, which,
again, is one of the strongest
arguments in the favor of Java.
All right.
That's it.
So end of these set of videos.
Next, we'll start building
an array-based list,
and I hope you liked
the philosophy.
