1 00:00:00,600 --> 00:00:03,370 One thing we should cover, before we get too far into array though, 2 00:00:03,370 --> 00:00:04,580 is one of memory. 3 00:00:04,580 --> 00:00:07,230 Now the Java language does a really great job 4 00:00:07,230 --> 00:00:11,002 of abstracting a way your need to think about how all these variables that 5 00:00:11,002 --> 00:00:13,840 we are creating are stored in the computer's memory. 6 00:00:13,840 --> 00:00:18,539 When you declare a variable and specify its type, what Java actually does for 7 00:00:18,539 --> 00:00:20,706 you is to reserve a spot in memory for 8 00:00:20,706 --> 00:00:24,414 you that is big enough to store your data on initialization. 9 00:00:24,414 --> 00:00:26,350 And that's super nice of it, isn't it? 10 00:00:26,350 --> 00:00:29,000 Not having to think about that sort of thing allows us to focus 11 00:00:29,000 --> 00:00:29,780 on the task at hand. 12 00:00:30,980 --> 00:00:33,600 Have you ever been to a movie theater with a group of people and 13 00:00:33,600 --> 00:00:35,078 had to reserve seats for them? 14 00:00:35,078 --> 00:00:37,480 You know, you usually put some stuff there like a sweater, 15 00:00:37,480 --> 00:00:41,790 maybe a purse to signify that these seats are for other people and they're saved. 16 00:00:41,790 --> 00:00:45,740 Now when you do that, you wanna make sure that the seats are contiguous, 17 00:00:45,740 --> 00:00:49,610 or all next to each other, one after the other. 18 00:00:49,610 --> 00:00:51,570 Now this is similar to arrays. 19 00:00:51,570 --> 00:00:55,800 Java needs to know how many elements your array going to have when it's created. 20 00:00:55,800 --> 00:01:00,010 So it can go and reserve the right amount of memory, just like those seats. 21 00:01:00,010 --> 00:01:05,260 It also requires your elements to be contiguous, one after the other in memory. 22 00:01:05,260 --> 00:01:06,850 So what do you say, we go reserve those seats. 23 00:01:07,960 --> 00:01:10,804 So go ahead and launch the workspace attached to this video. 24 00:01:10,804 --> 00:01:15,740 And then what we'll do is we'll start up jshell, so we can explore a bit. 25 00:01:15,740 --> 00:01:20,380 So let's assume that I wanted to go to the movies with some coworkers, right? 26 00:01:20,380 --> 00:01:24,460 I'm out with Ben, Alana, and Pasan, and they're all running a little bit late. 27 00:01:24,460 --> 00:01:26,330 So I'm gonna grab us some seats. 28 00:01:26,330 --> 00:01:28,950 So let's do this, let's just store their names. 29 00:01:28,950 --> 00:01:33,552 So, we could declare a bunch of friend variables, right? 30 00:01:33,552 --> 00:01:40,626 I can call the first one here, we'll and say friend1 = "Pasan". 31 00:01:40,626 --> 00:01:43,055 .And then of course we'd have to make another one. 32 00:01:43,055 --> 00:01:47,034 And one thing that you can do is you can declare a variable without 33 00:01:47,034 --> 00:01:48,222 initializing it. 34 00:01:48,222 --> 00:01:49,350 Let's explore what happens. 35 00:01:49,350 --> 00:01:56,250 So if I say String frend2, and I'm just gonna declare it and not initialize it. 36 00:01:57,660 --> 00:02:02,570 So here, we'll see that the unitialized string object isn't null. 37 00:02:02,570 --> 00:02:06,390 So null is used to represent the absence of a value. 38 00:02:06,390 --> 00:02:09,250 Now if you haven't run into these yet, consider yourself lucky. 39 00:02:09,250 --> 00:02:13,050 They end up causing a lot of grief in the form of the dreaded 40 00:02:13,050 --> 00:02:14,880 null pointer exception. 41 00:02:14,880 --> 00:02:17,310 Here, let me introduce you to the pain. 42 00:02:17,310 --> 00:02:22,260 So, let's say that I came along and I didn't know that this variable here, 43 00:02:22,260 --> 00:02:26,710 this variable friend2, let's say that I didn't know that it was actually null. 44 00:02:26,710 --> 00:02:28,930 Let's assume that I thought there was a value in here, 45 00:02:28,930 --> 00:02:31,149 like you would when you saw a variable. 46 00:02:31,149 --> 00:02:32,546 So let's go ahead and use it. 47 00:02:32,546 --> 00:02:34,850 So we'll say friend2.toLowercase, 48 00:02:34,850 --> 00:02:39,670 cuz I wanna get the lowercase version of this name here, so let's see what happens. 49 00:02:42,580 --> 00:02:45,120 And boom, there it is, the null pointer exception. 50 00:02:45,120 --> 00:02:49,251 So what I wanna point out is that when you declare an object, right, so 51 00:02:49,251 --> 00:02:53,593 when we declare an object like this, and you do not initialize its value, 52 00:02:53,593 --> 00:02:55,740 it defaults to null. 53 00:02:55,740 --> 00:02:59,600 Primitive data defaults like int and bool, they have different default values, 54 00:02:59,600 --> 00:03:01,870 and we'll get to those defaults here shortly. 55 00:03:01,870 --> 00:03:03,490 Check the teacher's notes if you just can't wait. 56 00:03:04,570 --> 00:03:08,110 So we know that we don't want to create a variable for 57 00:03:08,110 --> 00:03:10,150 each of our friends, that's just silly. 58 00:03:10,150 --> 00:03:13,380 So what we want to do is use an array. 59 00:03:13,380 --> 00:03:18,500 And the way that you declare an array is by placing brackets after your type, 60 00:03:18,500 --> 00:03:19,020 like this. 61 00:03:20,050 --> 00:03:25,950 And we'll declare a new string array named friends. 62 00:03:25,950 --> 00:03:30,304 And what we need to do is we need to say that that's a new String array. 63 00:03:30,304 --> 00:03:34,500 And then you need to declare how many elements that you're going to need. 64 00:03:34,500 --> 00:03:36,970 And we need three, cuz we have three friends coming. 65 00:03:36,970 --> 00:03:39,136 We have Pasan, Elena, and Ben. 66 00:03:41,282 --> 00:03:45,348 What this output is showing us here in jshell is that each 67 00:03:45,348 --> 00:03:50,110 of the elements in the array has been defaulted to null. 68 00:03:50,110 --> 00:03:52,370 And that's just like when we declared the string and 69 00:03:52,370 --> 00:03:53,910 didn't initialize its value, right? 70 00:03:53,910 --> 00:03:59,810 It's null, so there's a three-element array filled with nulls. 71 00:03:59,810 --> 00:04:01,950 We'll get how to set these values here in a bit. 72 00:04:01,950 --> 00:04:02,930 Hold tight. 73 00:04:02,930 --> 00:04:06,460 For now let's take a look at a primitive data type example. 74 00:04:06,460 --> 00:04:08,670 Let's revisit that golf score idea. 75 00:04:08,670 --> 00:04:11,130 So we know that there are 18 holes and 76 00:04:11,130 --> 00:04:13,850 we are going to be storing the score for each of them. 77 00:04:13,850 --> 00:04:16,540 So we know that it's an int, right? 78 00:04:16,540 --> 00:04:19,000 So the score will be an int. 79 00:04:19,000 --> 00:04:22,102 And we want an array of them, so we put the brackets, and 80 00:04:22,102 --> 00:04:23,758 we're gonna name it scores. 81 00:04:23,758 --> 00:04:29,149 We're gonna say it's a new integer array, and it's gonna have 18 values. 82 00:04:31,270 --> 00:04:32,463 And there we go. 83 00:04:32,463 --> 00:04:37,409 18 elements, all set to the default value, which, as you can see here for 84 00:04:37,409 --> 00:04:38,680 integers, is 0. 85 00:04:38,680 --> 00:04:41,870 Having an array declared like this doesn't really do us much good if we can't 86 00:04:41,870 --> 00:04:42,760 set the values. 87 00:04:42,760 --> 00:04:44,440 So let's get to that, right after this quick break.