 Let's start chapter nine tonight and let's talk about primitives versus objects The book is actually pretty good on this and so rather than me having to redraw all of the diagrams I'm going to follow the book on this We've been talking about int double char and Boolean and these are what are called primitive types and This memory is stored directly So when I have int number becomes negative two and symbol is assigned the exclamation point You can think of the symbol number Or the name variable name number Directly storing the value negative two The variable name symbol directly stores the exclamation point When we have arrays and objects like strings, that's not the way they work again. They are by reference So if I have an array of characters with c a and t, this is what the memory diagram looks like Here's are my variable name and the variable name contains a reference To that array of characters and I guess it might be a good idea to go into j shell right here at the moment So if I say character Um, what do they call it here? Array, okay I don't know why they used an array of char, but okay fine. Well, we'll go with it And now I say system dot out dot print line of array Okay, dang that did not do what I want because unfortunately Print line does special stuff with characters. Let's make an integer array Although I need an equal sign Okay, there we go You know what? I think we're gonna have a problem because I think j shell is is getting in our way Nope, it isn't okay. This is what we were talking about before. It's a reference This square bracket means we have an array the i means its integer And the at sign gives us the address in memory I knew that I shouldn't use characters as my example Strings work the same way. So if I have string word is assigned dog Word is a reference to memory somewhere and somewhere in memory In fact, the area that it's in is called the heap somewhere in the heap is the string dog And we can usually create them with the new keyword which allocates memory for them We did that here. In fact, well, I didn't do that before so let's go back to j shell and I can say for example integer array Numbers is a new integer array Of length five And that creates five values all which happen to be initialized to zero And it says here when you want to compare strings for equality, you need the equals method And let me show you why that is the case by the way For example, let's say I have string. I think I may have done this before String one is going to be a new string And now I say string stir two Is another new string These happen to be in different areas of memory So if I say is string one the same reference as string two the answer is false Because there are two copies of the word dog. That's why I need to use the equals method To compare the things they refer to not the references themselves Um, we need to declare an object normally we've been assigning it a reference to some Value For example here I had a new integer of five earlier when I think I had um array It had three integers in it It turns out that when you have strings and arrays and other objects You can declare a variable that doesn't refer to any objects at all at least initially and that's with the null value And let's use this example here Let's have a string name and we're going to say no value yet And we're going to have an integer Array called a combo that has the value null To show you that it really doesn't have a value is what if I try okay, well, let's go back and Remember if I had string stir one What was this str one? Because there is a value I can look at string one dot length But since name does not have any value at all if I try and get the name of something that's null I'll get the dreaded null pointer exception I can't get the length of something that has not never been given any value I can't for example say system dot out Dot print line of combo sub zero Why because there is no zero with element there's no elements at all And again, I get a null pointer exception on that Um, sometimes we can pass them as an argument or receive one as a return value And we usually will do that if we want to represent a special condition or indicate an error The person who first invented null references by the way Characterizes it has his million dollar mistake Because there have been so many software programs that have gotten these null pointer exceptions where you're trying to do something and there's really no value to use And preparing all those bugs he figures has cost a billion dollars So I'm not thrilled with people using null indiscriminately If you do want to use null and pass it as an argument Or return it as a value from a method Be very careful about what you're doing and documented in a comment Other languages by the way have better ways of handling error conditions rather than using null to represent an error Um, but unfortunately java cannot take advantage of those Newer more I don't know if I want to say secure new or more robust methods of doing it But that's a That would be getting I could go into detail with that beginning as way off of chapter 9 Okay strings are immutable um Oh, yeah, if we didn't have a string class, we'd have to use arrays of characters to store and manipulate text and there are languages like that see by the way is one of them and Let's just say it's a mess to manipulate strings in languages that don't have a built-in string class So we are very glad that java has that available Now again, I think I mentioned this before very very important when we have this too lower case and too upper case It makes you think that it changes the value, but it doesn't And let's go and um do this example here So if I have string name this time, I'm going to give it that And then I'm going to string upper name becomes name dot to upper case We're going to use the uppercase method and invoke it with name as the object that we want to mop Transform Upper name is ellen turing, but name is still exactly the way it was before And again strings are immutable and now this is the thing that I get from everybody When I talk about this they say here we got a string metal I'm going to say metal becomes lead and then I say metal becomes gold Didn't I just change the string and the answer is no you did not change lead into gold Yeah, what's happening is this first assignment. Here's the memory diagram Metal refers to someplace out on the heap with the word lead inside of it When you have the second assignment, we change the reference So the reference is now referring to an area of the heap with the letters g old the word gold in it The string lead is still on the heap. It has not been modified. Nobody's referring to it anymore It's still out there though And there's a process called the garbage collector that will go through in the java virtual machine and get rid of and reclaim the memory from Data that is no longer being referred to by anybody else So the question is what do we mean when we say changing the string? Well, here's what we could do in some other languages I could say metal is assigned gold and then I could change element number zero of the string to the letter s But java won't let you do that because the string itself is Not modifiable However, if I do want to change the word gold to sold I can Create a brand new string which is the letter s And then the rest of the letters for metal and then reassign it to metal. That's okay. Again, I'm doing a reassignment I'm not changing the original. I'm creating a brand new string and reassigning the reference To refer to this new string that I have just constructed and This is a distinction that really does make a difference So strings are immutable. Okay wrapper classes Primitives are not objects So if I say int i becomes five, I cannot do something like this the compiler will complain Let's just check that out to make sure that's the case And then I want to say i dot equals five and the answer is Int cannot be dereferenced int isn't a reference. It's It's it's a primitive value So for each of these primitive types, we have a wrapper class. That's w r a p p e r not r a p p e r as in musician And the wrapper class for int is named integer with a capital t With a capital i excuse me. Oh What was I say? Well, I don't know where that came from Now notice this is an capital i integer i becomes integer dot value of five what this will do is it will take the value five which isn't int a primitive And wrap it up into an object and now I have an actual object and now I can say is that equal to five In fact, let's do this here as capital i integer and all to make it clear Let's call it int object is integer dot value of five And now I can do integer object equals five And everything is going to work out fine Because again, I now have an object. I don't have a primitive anymore And there's capital b boolean for booleans capital c character for char variables capital d double and capital l long which correspond to the primitives small b booleans small c char small d double and small l long And you don't have to import them. They're automatically available And these um objects are immutable. So you have to use the equals method to compare them This creates two different objects So if I try and compare their references, that's false But if I say do they have the same value? That's true And also by the way, these wrapper classes that this is very nice You can find out the minimum and maximum value for an integer So for example, what's the long dot max value Is this gigantic number here Long dot minimum value that How about double dot max value is 1.7 times 10 to the 308th 90 percent of the time you'll want to go with the regular primitives int char double and long However, this other question is well, why in the hell do we have this stuff anyway? And the reason we have these is because Later on and I'll put this in my notes here today So why wrapper classes? Answer Later on this semester And a lot in computer science 76 We will be using Classes that require you to have objects When you build them One of them this semester by the way, I could type it correctly is the array list class You can't build an array list of int But you can build an array list of Capital i integer And that's why we have these wrapper classes. We have them for those situations where Java requires an object and a primitive simply will not do Hey, we've seen that value of Takes a primitive integer and returns a wrapped object The question is how do we go the opposite direction? So I have capital integer int object becomes integer dot value of Let's say well, I'm 23 fine so we use value of to Convert a primitive int to an object integer I'm sorry. I'm having so much trouble typing tonight So go the opposite direction you use the int value method So I could say int primitive value would become int object dot int value And in fact, let's try that out in the shell here and then I should be able to say int primitive value becomes int object dot int value And I give back my primitive value now. This is the way we used to have to do this We had to do this all the time Every time we wanted to make sure we had an integer object We had to do a value of or we had to create a brand new Integer capital i integer with the new keyword And then every time we had this object and we needed the primitive value We had to make a called int value boy. What a mess that was Turns out that we no longer need to do that. This is how we can do it explicitly There's nothing wrong with doing it explicitly if you want to Okay So Seeding to statements Our explicit conversions Turns out that java is very smart And now that I'm not sure when it was introduced. I think around java 8 maybe We'll do what is called auto box and auto Unbox values So you don't have to go through all the trouble So I can say integer Int object becomes 123 and then the primitive Will be auto box Into an integer object And if I say int primitive value becomes integer object Then the object Will be auto unboxed Into a primitive value And in fact, let's copy this and And it works exactly the same So definitely take advantage of auto boxing and auto unboxing. They will save you a lot of Trouble when you have to deal with These things in the context where java actually requires an object. Okay. What is the next topic here? Command line arguments. I'm going to come back to that later. I think I'm not going to talk about big integer arithmetic I'm going to let you read about this on your own. I'm not going to cover it in the mini lectures It's important. It's good to know but let's leave that for a later point I'll come back to command line arguments. Perhaps later this evening or maybe tomorrow Let's go to the other book though and see what else it has to do. I do want to talk about Okay methods in the scanner class Please read this section on your own if you have any questions And you want me to talk about it later on in another mini lecture Yeah, send me an email and let me know and This is dealing with some of the minutiae of java itself How does java itself work and really what we want to do is we want to talk about big concepts in this course Like what are objects? What's a loop? How do I do selection if else that kind of stuff? But every once in a while we have to deal with the Inner workings and the little details And some things that will come up when you're generalizing and modularizing your code And so if you're going to hit basically what I'm telling you is this when you generalize a method for doing input Don't create a scanner inside of a method So if I have a method for Getting input and back here. Let's let's do this I mean as we'll go through it. Okay. Well, let's go through it since I'm talking about it In for a penny in for a pound So let's call this scanner Details dot java And this is show that scanner has some things you have to watch out for Namely Don't recreate Scanners if you can help it So let's say I do something like this public static int get positive number And here I'll say scanner Input is new scanner of System dot in and Yeah, I need to put up a prompt Run with this to be a do file loop Sure. Why not then I'm going to say n becomes input dot next int If n is less than or equal to zero Do that as long as n is less than or equal to zero And then I'll return and There we go I think that's rather nice And then here I'm going to do something like integer Number one is going to be get positive number In number two is going to be do get positive number And then we'll have a double average becomes number one Plus number two Plus number three divided by three point zero. Oh, oh goodness. What happened here illegal start of expression Oh, yeah, I've left a blank between the less than and equal sign How about printf? Excellent. So now on an executive is a negative three Find seven five and twenty two That was 11.33. Oh see Hey, well, where's the problem lovely everything works like a champ Except for one problem someone's going to say hey here you close your input First of all, I'm creating a scanner here, which I never use which is sort of weird and I close it here Well, now I'm creating a scanner every single time That I call this method so Problem one and there's some overhead involved. It's it's a bit ugly problem two um If I close the scanner inside this method Oh boy, am I in trouble So somebody will say oh you didn't close your scanner. Oh, wow, that's right. You say and you do input and dot close There problem solved And now let's run it and if I say enter a number of five And all of a sudden this has no such element exception And the reason is because once you close a scanner you can no longer open it again Which means I can't open a connection to the keyboard twice in a program I've already closed it and I'm out of luck. So let's save this as scanner Details better, but let's go Um correct dot java And what this time I'm going to do is I am going to create the scanner here in main That's perfectly okay, and I'm going to pass it on here Now I don't have to declare it And I don't have to close it either I'm going to close the scanner once I'm completely finished with it at the end of the program And every time I want a positive number I'm going to pass in this a reference to this scanner that I've already created Create And accidentally close And now I have five seven 28 And now it still works. So this is just a A heads up about what happens when you're passing a When you have a scanner and you want to do input From a method several times pass the scanner in do not create new scanners every time And since this is now correct I don't have the problems anymore. So let me change my comment here my parameters So I'm free to use it method that called me Will take care of creating and closing it won't do it here Yeah, that's much better. And the next thing I want to talk about is the software development cycle so This is our two step cycle when we've been writing programs so far you write it and you test it If it doesn't work you go back to step one Try writing again. There's actually a step zero here, by the way We just plan the damn code, but let's let's leave that out for right now A lot of the planning has been done for you when I write up the assignment Most of the assignment tells you what the plan is because it tells you what things you need to do But we've got to write and test that's that's our cycle so far The software development cycle has a lot of different definitions And I've seen some with five steps some with as many as 10 But they all have the idea that there's more than just writing and testing writing and testing until you get what you want So this is a very nice seven step definition What's the problem you're trying to solve and what does the customer want the program to do? You would be surprised how many times I've seen people write programs and they don't have an idea of what they really wanted to do when it grows up And that is not a recipe for success So you find I say why are we using a computer in the first place? Maybe this is a problem that could be better solved by hand or by some other process And again, what does the customer want? Once we know what the customer wants and what problem we're trying to solve now we can say, okay What does the program actually need to do? How do we solve the problem using a computer? and or and You'll have to talk to the end users So you're just going to ask them, okay Well, do you want this for example dialogue boxes if we're using a web application or would you rather have you know Free text or you know like it's an example If you're going to enter the month Do you want to be entering it by a month by a number or by a drop down menu with the names of the months january through december? Which do you prefer? So that's the kind of thing that would happen in system analysis and design So now that you know what that is You can go to your third step which is system design And now you develop the data structures and the methods that you need to Do the tasks that the application is supposed to perform And you write this all down. You haven't really started coding yet That's in the development phase Okay, then we do Testing to see if the program works the way you said the requirements have and this is best done by a group of people Who did not write the code So the developers are the worst people to do the testing When you do testing you want actual users of the program or you want someone else Okay, this isn't again in a large project for the assignments here. You are both the You're you're the designer. You're the writer and you're also the user all three at once And then you're also going to be the person who's testing things Okay, after you've tested and you've gotten rid of the bugs that you know about Then it's time to deploy the program Send it out to the end users Now guarantee that they will find things that you did not find which means we are going to have bug fixes and feature enhancements And say well either something doesn't work or gosh, this works great, but can you make it do something extra? Or yeah, gosh, this works, but it seems a little bit too difficult. Can you make it easier for us to do that? And every one of these stages We may have to go back earlier and earlier for example during testing. We might find out that we have done something Yeah, we're satisfying the requirements, but our algorithm is way too slow And that means we have to go actually back to step to step three here saying we need a different method to solve this problem Because the one we first decided on was too slow to work To our users user satisfaction satisfaction Sometimes the users during testing We'll say This is not what we had in mind at all So we might have to go back to step two or even all the way back to step one They're saying yeah, this is nicely nice. It's great. Great at solving a problem But it's not a problem that we ever encounter. So who cares? So be aware that this is the software development life cycle And um There I believe somewhere later on there's a discussion Assignment that where you're going to be writing about that Let's see what's back here Uh, I don't want to talk about command line arguments okay Right now we've been using the integrated development environment to do all of our Um writing of the programs and running them There are other ways to do it and you can do this from the command line. Okay, this is sort of advanced stuff I don't think it's going to show up on the exam, but it's useful. It's interesting to know and that's why I'm talking about it Namely we're going to figure out what this string args does So let's say what I want to do here is I'm going to pass some command lines. We want to find the maximum value in a sequence of numbers So let's copy this and let's Open up our template Let me see what this says it's supposed to do Find the maximum value. Okay, cool. Let me just paste this in here so I can see what's the difference We're not going to use a scanner in this case And we're going to need here how to use the command line To get arguments From the user let's compile that to make sure that compiles now The problem is if I run it it's not going to do what I want eventually So let me show you how we're going to run this from the command line And let's do an ls to list all my files. You'd use a different command if you were in windows, by the way Well, that's interesting Excuse me. I need to figure out where I put Oh, lovely. I have been storing all of these in the wrong Directory so excuse me for a moment while I fix that Okay, there now they're in the right place Um, so if I list my files, you'll notice I have max dot java and it turns out I also have max dot class That is the java bytecode And so now I can say let's go and run java And I want to use the max program And what it'll do is it will show us all the arguments what I want to be doing Here's what I want as a result. What I want to be able to do is I want to be able to print out things like 3, 8, 22 117 and I want to get the maximum of those In fact, as long as I've got this, this is perfect. I may as well do here and you'll notice Here I have this array. Now. These are strings. They aren't numbers. They're actual strings in here I can put anything I want here in fact and having so much trouble typing tonight So whatever I put after the name of my program that goes into the args array And there are a bunch of strings Okay Cool. Now that I know that What I can say here is I want my maximum value And I'm going to set it To be integer dot minimum value And that guarantees That everything I look at will be bigger Now what I'm going to say is for i is zero i is less than args dot length i plus plus My plan is to convert args sub i from a string To an int and then compare it to the maximum value If it's greater That becomes the new maximum value Now the question is how do I Take a string and convert it to an integer and the answer is there's a there's a method for that So I can say integer value becomes Integer dot parse int arg sub i So what we'll do is it will take each of the values arg sub i and parse it as an integer And that will convert it to an int Then if the value is greater than the maximum value That becomes the new maximum value And when I'm done with my loop, let's compile that now How would I compile that from the command line? The answer is I would say java compiler java c max dot java Doesn't gave me no output that means everything was great Let me show you what happens if I make some sort of an error in here like this That's interesting Did I save this? Yes, I did Uh, hold on a second while I look I'm gonna pause the recording for a second Sorry, I had the I had it in the wrong folder here. Let's do again java c max dot java And there I got my error message So let's go back here Put that back And compile it Oh, that's interesting Okay, looks like it's time for me to look up what the minimum value is I could have sworn it was I wonder was val or value. Well, let's find out here It's min value not min val save the file here This is what we used to have to do by the way before integrative development environments came along And now let's try java max with 10 to four 66 47 and 11 And the maximum value is 66 So that's a brief view of how you can read Strings from the command line Those are going to be in the args array. Those are the arguments that are going to your program You can convert them using parse int if we had had floating point numbers We would have used capital d double dot parse double And then you can do whatever you want with those arguments. So Nice to know I guess It's in the book and that's why I am talking about it um I'm going to skip big a big big integer arithmetic completely Incremental design you might want to again read this one on your own read 9.8 and 9.9 if you have questions Send me them in canvas and I will address them tomorrow Otherwise what I think I'm going to do is I'm going to head straight on To chapter 10 And the reason I'm doing that is because I'm not going to be available Um next week monday and tuesday. So I want to get a head start on chapter 11 Which is a really important one on wednesday and thursday. So my plan is to talk about chapter 10 here Um tomorrow And maybe a little bit of wednesday, but wednesday and thursday I'll be able to start talking more about uh chapter 11 See you tomorrow