Check out a free preview of the full Java Fundamentals course

The "Arrays Q&A" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie demonstrates writing the printTicket for loop using i and discusses why a do while loop was used instead of a while loop. Student questions regarding how to print on one line and if the terms mutable and immutable are used in Java are also answered in this segment.

Preview
Close

Transcript from the "Arrays Q&A" Lesson

[00:00:00]
>> If you were to have written this for Loop, I'll write it here and just comment it out. But if you were to use this with i, Another thing, so we could have said, the hard coded constant value. But another way, sometimes you won't have those values defined, you have a method that accepts an array, you don't know what this array has in it.

[00:00:26]
So you can do something like array.length, and that'll give you the number of elements that's in that array, and then i++. And then inside of here I would have just printed out ticket, no, not ticket, it's called array, right? Array i+, and then this little pipe thing. So I would have accessed the element by its index.

[00:00:58]
So this is another way to do it. I'll just comment it out. Okay? Questions on arrays? Pretty clear? Why did we use the do while instead of a while here?
>> Cuz you wanted it to run at least once.
>> Yeah, we want to run at least once.

[00:01:32]
So you see, there are valid cases. So when people are like, well, why ever use a do while? I have a little problem with that, this makes logical sense to do that, right? Why would I search to see if the number has been there, if I haven't even generated the number first, all right.

[00:01:46]
>> What did you do to make the numbers pronounced the same line again?
>> Yeah, instead of print in, it's just print, yeah.
>> I guess there is one, are the terms mutable and immutable used in Java?
>> Yes.
>> Kinda related to the constant?
>> They are, and we actually gonna talk about immutability in this very next section I'm gonna go over, but yes, they do exist in Java.

[00:02:16]
>> The other part is, right now we're going between 1 and 68, so I think that's what we could also just.
>> Very good, okay, great. So, all right. So, what's your name?
>> Dave.
>> Dave pointed out that this bound here is not inclusive. So when we said next in, it's only gonna do one, if origin is included, is this inclusive?

[00:02:47]
>> That's inclusive, yeah.
>> Okay, so the first parameter is inclusive, meaning it will be a possibility, but the last one is a cap, meaning it will not. So we could do something like +1 right here. And that way we include max ticket number, so our bound would be 70, right?

[00:03:12]
>> That feels kinda like it works and it feels like you're just very kludgy in terms of how it's implemented.
>> You could change this to this, [COUGH] 70 right here.
>> And then just have a comment about it just saying inclusive of bounds or something like that.

[00:03:25]
Yeah, what I would actually do, I wouldn't do a origin. What I would do personally, I would do max number, let it just create it and then I will do +1.
>> Okay, that feels a little bit better.
>> Like that? And the reason I would do that is because I don't want 0 included either.

[00:03:47]
So that really works on either of the terms. So create a number between 0 and 68, 69 somewhere in between it and add add one to it, right? That way, that's my range.
>> Java Docs.
>> Java Docs, yeah, Java Docs are great. Just for the sake of time, we haven't been writing any on our own.

[00:04:15]
But we should, and to create one, it's really simple. Right above the method itself you would do, I can't remember if this is slash or backslash. Is that back or is that front? Whatever that is, some sort of slash, [COUGH] with an asterisk. And then hit enter, and that's the closing one.

[00:04:38]
But in that Java doc, you would then specify like, what does this method do you have a little one line there and then you would enter the parameters it takes, what it returns and a nice little description? So that's being a good developer. All right so, that is a race.

[00:05:01]
>> How did you access the Java doc?
>> I clicked on, [COUGH] so let's go to a method that this outside of our class, the random may be. So this random.nextInt. I click command, hold command down and then click it, and that takes me into here and you can see how the Java doc is written.

[00:05:29]
Basically, what is this? Slash two asterisks, and then notice it has the comments, and then it has the parameters. I don't know why it has all that, that's a lot, [LAUGH] this is a book, here's the parameters, so you would do @param. And then you could specify the parameters, a little bit of information about them, exclusive, the upper bound must be positive.

[00:05:59]
Those are good things to know, that helped us in our code, right? And whatever it'll be returned you let them know that.
>> I bet you the Java doc for var is probably even more convoluted given how much the guessing it's doing other kinds of logic you need to guide that.

[00:06:15]
[LAUGH]
>> Up there is a dark.
>> Wouldn't be? So they usually are on methods and var is not let's see.
>> What else we still kinda internal logic for like-
>> Yeah, I mean there's Doc's written on, I just don't know if it's in the form of Java Docs, but there I'm sure there's something on Oracle written on it, let me see.

[00:06:39]
Yeah, there's nothing here.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now