This course will be retired on March 31, 2026. We recommend "Java Arrays" for up-to-date content.
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 (2017)!
      
    
You have completed Java Arrays (2017)!
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 we get too far into array though,
                      0:00
                    
                    
                      is one of memory.
                      0:03
                    
                    
                      Now the Java language
does a really great job
                      0:04
                    
                    
                      of abstracting a way your need to think
about how all these variables that
                      0:07
                    
                    
                      we are creating are stored
in the computer's memory.
                      0:11
                    
                    
                      When you declare a variable and specify
its type, what Java actually does for
                      0:13
                    
                    
                      you is to reserve a spot in memory for
                      0:18
                    
                    
                      you that is big enough to store
your data on initialization.
                      0:20
                    
                    
                      And that's super nice of it, isn't it?
                      0:24
                    
                    
                      Not having to think about that
sort of thing allows us to focus
                      0:26
                    
                    
                      on the task at hand.
                      0:29
                    
                    
                      Have you ever been to a movie
theater with a group of people and
                      0:30
                    
                    
                      had to reserve seats for them?
                      0:33
                    
                    
                      You know, you usually put some
stuff there like a sweater,
                      0:35
                    
                    
                      maybe a purse to signify that these seats
are for other people and they're saved.
                      0:37
                    
                    
                      Now when you do that, you wanna make
sure that the seats are contiguous,
                      0:41
                    
                    
                      or all next to each other,
one after the other.
                      0:45
                    
                    
                      Now this is similar to arrays.
                      0:49
                    
                    
                      Java needs to know how many elements your
array going to have when it's created.
                      0:51
                    
                    
                      So it can go and reserve the right
amount of memory, just like those seats.
                      0:55
                    
                    
                      It also requires your elements to be
contiguous, one after the other in memory.
                      1:00
                    
                    
                      So what do you say,
we go reserve those seats.
                      1:05
                    
                    
                      So go ahead and launch the workspace
attached to this video.
                      1:07
                    
                    
                      And then what we'll do is we'll start
up jshell, so we can explore a bit.
                      1:10
                    
                    
                      So let's assume that I wanted to go to
the movies with some coworkers, right?
                      1:15
                    
                    
                      I'm out with Ben, Alana, and Pasan, and
they're all running a little bit late.
                      1:20
                    
                    
                      So I'm gonna grab us some seats.
                      1:24
                    
                    
                      So let's do this,
let's just store their names.
                      1:26
                    
                    
                      So, we could declare a bunch
of friend variables, right?
                      1:28
                    
                    
                      I can call the first one here,
we'll and say friend1 = "Pasan".
                      1:33
                    
                    
                      .And then of course we'd
have to make another one.
                      1:40
                    
                    
                      And one thing that you can do is
you can declare a variable without
                      1:43
                    
                    
                      initializing it.
                      1:47
                    
                    
                      Let's explore what happens.
                      1:48
                    
                    
                      So if I say String frend2, and I'm just
gonna declare it and not initialize it.
                      1:49
                    
                    
                      So here, we'll see that the unitialized
string object isn't null.
                      1:57
                    
                    
                      So null is used to represent
the absence of a value.
                      2:02
                    
                    
                      Now if you haven't run into these yet,
consider yourself lucky.
                      2:06
                    
                    
                      They end up causing a lot of
grief in the form of the dreaded
                      2:09
                    
                    
                      null pointer exception.
                      2:13
                    
                    
                      Here, let me introduce you to the pain.
                      2:14
                    
                    
                      So, let's say that I came along and
I didn't know that this variable here,
                      2:17
                    
                    
                      this variable friend2, let's say that I
didn't know that it was actually null.
                      2:22
                    
                    
                      Let's assume that I thought
there was a value in here,
                      2:26
                    
                    
                      like you would when you saw a variable.
                      2:28
                    
                    
                      So let's go ahead and use it.
                      2:31
                    
                    
                      So we'll say friend2.toLowercase,
                      2:32
                    
                    
                      cuz I wanna get the lowercase version of
this name here, so let's see what happens.
                      2:34
                    
                    
                      And boom, there it is,
the null pointer exception.
                      2:42
                    
                    
                      So what I wanna point out is that
when you declare an object, right, so
                      2:45
                    
                    
                      when we declare an object like this,
and you do not initialize its value,
                      2:49
                    
                    
                      it defaults to null.
                      2:53
                    
                    
                      Primitive data defaults like int and
bool, they have different default values,
                      2:55
                    
                    
                      and we'll get to those
defaults here shortly.
                      2:59
                    
                    
                      Check the teacher's notes
if you just can't wait.
                      3:01
                    
                    
                      So we know that we don't want
to create a variable for
                      3:04
                    
                    
                      each of our friends, that's just silly.
                      3:08
                    
                    
                      So what we want to do is use an array.
                      3:10
                    
                    
                      And the way that you declare an array
is by placing brackets after your type,
                      3:13
                    
                    
                      like this.
                      3:18
                    
                    
                      And we'll declare a new
string array named friends.
                      3:20
                    
                    
                      And what we need to do is we need to
say that that's a new String array.
                      3:25
                    
                    
                      And then you need to declare how many
elements that you're going to need.
                      3:30
                    
                    
                      And we need three,
cuz we have three friends coming.
                      3:34
                    
                    
                      We have Pasan, Elena, and Ben.
                      3:36
                    
                    
                      What this output is showing us
here in jshell is that each
                      3:41
                    
                    
                      of the elements in the array
has been defaulted to null.
                      3:45
                    
                    
                      And that's just like when
we declared the string and
                      3:50
                    
                    
                      didn't initialize its value, right?
                      3:52
                    
                    
                      It's null, so there's a three-element
array filled with nulls.
                      3:53
                    
                    
                      We'll get how to set these
values here in a bit.
                      3:59
                    
                    
                      Hold tight.
                      4:01
                    
                    
                      For now let's take a look at
a primitive data type example.
                      4:02
                    
                    
                      Let's revisit that golf score idea.
                      4:06
                    
                    
                      So we know that there are 18 holes and
                      4:08
                    
                    
                      we are going to be storing the score for
each of them.
                      4:11
                    
                    
                      So we know that it's an int, right?
                      4:13
                    
                    
                      So the score will be an int.
                      4:16
                    
                    
                      And we want an array of them,
so we put the brackets, and
                      4:19
                    
                    
                      we're gonna name it scores.
                      4:22
                    
                    
                      We're gonna say it's a new integer array,
and it's gonna have 18 values.
                      4:23
                    
                    
                      And there we go.
                      4:31
                    
                    
                      18 elements, all set to the default value,
which, as you can see here for
                      4:32
                    
                    
                      integers, is 0.
                      4:37
                    
                    
                      Having an array declared like this doesn't
really do us much good if we can't
                      4:38
                    
                    
                      set the values.
                      4:41
                    
                    
                      So let's get to that,
right after this quick break.
                      4:42
                    
              
        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