Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed Java Arrays!
      
    
You have completed Java Arrays!
Preview
    
      
  Let's take a look at how to declare new arrays.
Default values for types
- 
byte:0
- 
short:0
- 
int:0
- 
long:0L
- 
float:0.0f
- 
double:0.0d
- 
char:'\u0000'
- 
String (or any object):null
- 
boolean:false
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
                      One thing we should cover before
                      0:01
                    
                    
                      we get too far into arrays is memory.
                      0:03
                    
                    
                      Now, the Java language does
a really great job of abstracting away
                      0:06
                    
                    
                      your need to think about
                      0:09
                    
                    
                      how all these variables we're creating
are stored in the computer's memory.
                      0:11
                    
                    
                      When you declare a variable
and specify its type,
                      0:15
                    
                    
                      what Java actually does is reserve
a spot in memory
                      0:18
                    
                    
                      that's big enough to store
your data upon initialization.
                      0:21
                    
                    
                      And that's super nice of it, isn't it?
                      0:25
                    
                    
                      not having to think about that
                      0:27
                    
                    
                      sort of thing
allows us to focus on the task at hand.
                      0:28
                    
                    
                      Have you ever been to a movie theater
with a group of people
                      0:32
                    
                    
                      and had to reserve seats for them?
                      0:35
                    
                    
                      You know, you usually put some stuff
there, like a sweater, maybe a purse,
                      0:37
                    
                    
                      to signify that these seats
are for other people and they're saved.
                      0:41
                    
                    
                      When you do that,
                      0:45
                    
                    
                      you want to make sure
                      0:46
                    
                    
                      these seats are contiguous, or all
next to each other, one after the other.
                      0:46
                    
                    
                      Now, this is similar to arrays.
                      0:51
                    
                    
                      Java needs to know how many elements
                      0:53
                    
                    
                      your array is going to have
when it's created so it can go
                      0:56
                    
                    
                      reserve the right amount of memory,
just like those seats.
                      0:59
                    
                    
                      It also requires that your elements are
contiguous, one after the other, in memory.
                      1:02
                    
                    
                      So what do you say
we go reserve those seats?
                      1:07
                    
                    
                      Go ahead and launch the workspace
attached to this video.
                      1:12
                    
                    
                      We'll start up jshell
so we can explore a bit.
                      1:15
                    
                    
                      Let's say I'm out at the movies
and I'm going to meet up with
                      1:19
                    
                    
                      some friends, Brian, Rohald, and Laura,
and they're all running a little bit late.
                      1:22
                    
                    
                      I'm going to grab us some seats.
                      1:27
                    
                    
                      Let's store their names.
                      1:29
                    
                    
                      We could declare a bunch of friend
variables, right?
                      1:31
                    
                    
                      I can call the first one friend1
and say friend1 equals Brian.
                      1:33
                    
                    
                      And of course,
we'd have to make another one.
                      1:39
                    
                    
                      One thing you can do is declare a variable
without initializing it.
                      1:42
                    
                    
                      Let's see what happens then.
                      1:45
                    
                    
                      So if I say string friend2, and
I just declare it without initializing it,
                      1:47
                    
                    
                      here we'll see that
the uninitialized string object is null.
                      1:53
                    
                    
                      Null is used
to represent the absence of a value
                      1:57
                    
                    
                      Now if you haven't run into these yet,
consider yourself lucky.
                      2:01
                    
                    
                      They end up causing a lot of grief
in the form of the dreaded null pointer
                      2:04
                    
                    
                      exception.
Here, let me introduce you to the pain.
                      2:07
                    
                    
                      Let's say I didn't know that this variable
here, friend2, was actually null.
                      2:11
                    
                    
                      Let's assume
                      2:15
                    
                    
                      I thought there was a value in it, like
you might expect when you see a variable.
                      2:15
                    
                    
                      So I go ahead and use it.
                      2:19
                    
                    
                      Friend2.toLowercase.
                      2:21
                    
                    
                      I want to get the lowercase version
of this name here.
                      2:24
                    
                    
                      And boom, there it is.
                      2:26
                    
                    
                      The null pointer exception.
                      2:28
                    
                    
                      So the point I want to make is this.
                      2:30
                    
                    
                      When you declare an object, like a string, and
don't initialize it, it defaults to null.
                      2:32
                    
                    
                      Primitive data types, like 
int and boolean,
                      2:38
                    
                    
                      have different default values,
and we'll get to those shortly.
                      2:40
                    
                    
                      Check the teacher's
notes if you just can't wait.
                      2:43
                    
                    
                      So we know that we
                      2:46
                    
                    
                      don't want to create a separate variable
for each of our friends.
                      2:47
                    
                    
                      That's just silly.
                      2:50
                    
                    
                      What we want to do is use an array.
                      2:51
                    
                    
                      The way you declare an array is by placing
square brackets after your type,
                      2:54
                    
                    
                      like this. We'll declare a new string
array named friends
                      2:58
                    
                    
                      And we say equals
                      3:04
                    
                    
                      New string, three. We need three elements
                      3:06
                    
                    
                      because we have three friends
coming: Brian, Rohald and Laura.
                      3:10
                    
                    
                      What the output shows us here in JShell
                      3:15
                    
                    
                      is that each of the elements in the array
has been defaulted to null.
                      3:17
                    
                    
                      Just when we declared that single string
and didn't initialize it, it's null.
                      3:21
                    
                    
                      So now we've got a three
element array filled with nulls.
                      3:25
                    
                    
                      We'll get to how to set these values
in just a bit.
                      3:29
                    
                    
                      Hold tight.
                      3:31
                    
                    
                      For now, let's take a look
at a primitive data type example.
                      3:32
                    
                    
                      Let's revisit that golf score idea.
                      3:35
                    
                    
                      We know there are 18 holes,
and we're going to be storing the score
                      3:38
                    
                    
                      for each of them.
                      3:41
                    
                    
                      So we'll use int, and we want an array,
so we'll put our square
                      3:42
                    
                    
                      brackets, and name it scores.
                      3:46
                    
                    
                      We'll say it's a new
                      3:49
                    
                    
                      integer array with 18 values.
                      3:50
                    
                    
                      And there we go!
                      3:54
                    
                    
                      18 elements, all set to the default value,
which,
                      3:55
                    
                    
                      as you can see here for integers, is zero.
                      3:58
                    
                    
                      Having an array declared
this doesn't really do us much good
                      4:01
                    
                    
                      if we can't set the values, so let's
get to that right after this quick break.
                      4:04
                    
              
        You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up