1z0-808 Oracle Java SE 8 Programmer – Java Syntax Part 2
- Operators: Part 2
Let’s look at a few more operators. This is the post and pre increment operator, where you see the plus. If you saw a minus, that would be the decrement operator. And what it does is it either increases for the increment or decreases for the decrement the current value of a variable. But whether it’s to the right side of the variable or the left side of the variable doesn’t does make a difference. And here’s what I mean. Whenever we have this operator, it’s going to assign a new value, in this case to X, but it’s also going to return a value to be used in an expression. So here we’ve got into X equals zero. When we say x plus the old value is zero, the new value is one. So X is now equal to one, but then it returns the old value to be used in an expression, the pre increment operator. Now the old value is one because we’ve incremented it.
The new value is two and it returns the new value. So let’s see how that would manifest itself. Here we’ve got a system out print line and we’re doing an X plus. It’s going to print out zero because it’s going to print out the old value. Then when we print out X again, it’ll print out the new value because X has that new value of one. Here’s the pre increment operator, so it will print out two. And when we print out X again, it has already been incremented in the previous print line, so it stays at two. And this is one of those things that you’re going to want to play around with, especially if you’re going to be taking the exam. Just sit down and start doing things like I’ve got into Y, it’s equal to zero. And what happens if I do plus Y, and now I’m going to print out Y.
All right, so what would happen if I ran that? All right, so we can see it was 0122 just like we expected, and here it’s printing one and that should be what you expected, right? That it’s going to print out the number one. We started with zero, we did a pre increment and now it’s printing out one. What do you think will happen if I change that to the other side? Will it print out zero or will it print out one? And I’m just going to comment these out so that we don’t get all that other stuff and run it and it also prints out one. Now why is that? The reason is that Y here is doing the post increment, which means the old value is zero, the new value is one, and then it’s returning the old value here to be used in an expression, but it’s not really being used in an expression. So that’s it, it returned the value.
Its current value is returned the old value, but its current value is now one. So once we print it out, it’s already been incremented, so it prints out the new value. How about this? This will be a little strange. What do you think will happen here? All I did now is say y is equal to y plus. We’ll run that. Now y is zero. Did the increment not happen? What occurred? Well, here’s what happened. Y is zero. So y goes through the post increment process. The old value is zero, the new value is one. Return the old value. So it returns the old value of zero and assigns it back to itself. So effectively, we just lost the new value and when we print it out, y will be equal to zero. All right, let’s look at another operator. Here is the ternary operator. It is a shorthand for doing a boolean expression. So, for example, if I’ve got a string that I’m creating here, I’m checking to see if is sleeping is true. If it is, we’ll set status to be sleeping, else status will be working.
We can do this all in one line with a ternary operating. It looks like this. You put your conditional check first and then you have a question mark. Anything after the question mark is what should be returned if is sleeping is true. And then the colon is everything after the colon is what should happen if is sleeping is false. This is your else. So that’s just a nice shorthand. It’s called the turn berry operator. And finally, just one other note about precedence. And again, if you’re taking the exam, spend some time looking at that table that I showed you so you can remember all the different orders of precedence. So with this first one into x equals three plus two times five.
Because multiplication occurs first, it would be five times two plus three. So the value be 13. We can influence the order, however, using parentheses. So if I wrap three plus two in this way, then that will be executed first because the parentheses have the highest order of precedence. So now it’ll be three plus two times five, which would give us 25. One of the things I tend to do is even though this wouldn’t change the outcome, I’ll often still wrap what I want to have happen first in parentheses, just to make it really clear to the reader what the order is, what’s going to be called first, what’s going to be called second, and so on.
- Loops
We just have a couple of other things to talk about in regard to loops. We’re going to look at two different keywords. We’re going to look at continue, and we’re going to look at break. And then we’ll also talk about labels. So take a look at the code that we’ve got on lines five through eight. Pretty simple. We are setting up an into on line five here to be I equals zero. On line six, we’re starting our while loop. We’re checking to see if I is less than three. And on line seven, what we’re doing is we’re printing out the value of I. But we’re not just printing out I, we’re also incrementing I. That is the post increment operator. So what that means is it will return the current value of I and then increment it afterwards. So instead of seeing the numbers one, two and three, we’ll see zero, one and two. Hopefully you’re able to see why in fact, I’ll just run both. So if I run this, we see down here zero, one and two. But if I change it to be a pre increment operator, then the value of I is going to be incremented before it prints it out, in which case we’ll see one, two, three. All right, let’s move that back to I plus. And what I’m going to do now is change this while to be true.
Now, is that legal? Sure it is. Look, there’s no compile errors, no red X’s in eclipse here. So remember that anything that’s in the while loop can be a bullion and a bullion literal, certainly a bullion value. So what do you think would happen if I ran this? You probably made a pretty good guess that it’ll just keep running, right? And you’re right. So you can see it’s just going to keep running because there’s nothing to stop this condition. So I’m going to hit stop since that’s not what I want to do. But I’m going to keep this true here. I want to show you another way to get out of a loop. And so what I’m going to do is put a check in here and I’m going to say if I is equal to ten, then I want to break. And so we’re starting at zero. We’re going to keep incrementing.
Once it gets to ten, then I want to break. And so what the keyword break does is it says whatever loop I’m currently in, I want to break out of it. So this is the loop we’re in, which means that once we hit break, once I is in fact equal to ten, then we continue on after that loop, and it will continue on with the rest of the code. So we’ll run that and it went from zero to nine. The zero is hidden because it scrolled off, but it did say zero. So that’s another way that we can break out of a loop using the keyword break. Let’s look at the word continue and see how that works.
Scroll down here, get another example. So here we’ve got a for loop and what we’re doing is we’re starting with K equal to zero. That’s our initialization section. We’re then checking to see if K is less than 100. And if it is, then do whatever is inside of this four block. After the code in the block has been executed, we’re going to increment K by one. So that should be fairly clear. Next what we’re doing is we’re checking here with an. If we’re saying if K is evenly divisible by seven, then we want to continue. Now what does continue mean? Continue means that we should continue with the loop. We’re not breaking out. But don’t bother doing anything below. Don’t do any of the additional statements that are inside of this for loop. So if K is evenly divisible by seven, it just goes to the next iteration of the loop. Let’s say it’s not evenly divisible by seven. Well then it’s going to print out this message. K is not divisible by seven. And if I run that you can see that it went through and it printed out all of the different numbers that are not evenly divisible by seven. So that’s continue. Continue says go to the next iteration of a loop. Do not execute any more statements for this current iteration.
Now I have one other example for you to look at and it’s going to be a little bit of a challenge for you. What I’d like you to do is to pause the video for a second and take a look at the code in lines 42 through 51 and try to make a guess as to what it’s going to do. So pause the video and come back once you’ve figured it out. All right, let’s take a look and see what it’s going to do. I’m just going to run it and see if it’s doing what you thought it would do. I don’t know if you can really tell, but it is continually going and it’s starting over and over again, going through the numbers zero through nine, over and over again, never stopping.
Now why is that? Well, let’s start with line 42. So we’ve got our while and we’re saying while it’s true now, when will that not be true? It’s always true. It’s the bullying true, the literal value. And so that is always going to evaluate to be true. So it’s going to go into this loop and it’s going to set Z to be zero. And now we’ve got another nested loop which is always set to true as well. So that is checking, it saying, hey, if Z is greater than or equal to ten, then I want you to break. Otherwise let’s print out the value of Z and increment it. So why isn’t it breaking out? Well, in fact it is breaking, but it only breaks out of the current loop. So once Z is equal to ten, then this loop on line 44 is done executing. It continues on at the end of that loop, which is actually still in this other loop.
So the loop on line 42 evaluates once again and says, is this true? And the answer is yes. And it resets Z to be zero again. So that’s why it’s constantly reset and why it continues forever. So how would we get out of both loops? Break is clearly not doing it. We have something called a label, and it’s pretty simple to use. What you do is you put some sort of text right above the loop that you want to break out of. So I’m going to say outer underscore, label, and then you put a colon. And what we’ve done now is we’ve created a label for this loop. And you don’t have to put label.
Some people put loop, some people don’t put anything. But by convention, the words are typically separated with an underscore, and by convention, they’re typically all lowercase. I guess I’ve seen both all uppercase and all lower case, but most of the people I work with use all lower case. And then what we need to do is reference that label in our break statement. So I simply say break, outer, label. And now when we run it now it’s just going from zero to nine. Again, the zero scrolled off, but trust me, it was there. And now when we hit Z is greater than or equal to ten, it breaks all the way past the outer label and continues on with the rest of the code.
- Break, Continue, and Labels
We just have a couple of other things to talk about in regard to loops. We’re going to look at two different keywords. We’re going to look at continue, and we’re going to look at break. And then we’ll also talk about labels. So take a look at the code that we’ve got on lines five through eight. Pretty simple. We are setting up an int on line five here to be I equals zero. On line six, we’re starting our while loop. We’re checking to see if I is less than three. And on line seven, what we’re doing is we’re printing out the value of I. But we’re not just printing out I, we’re also incrementing I. That is the post increment operator. So what that means is it will return the current value of I and then increment it afterwards. So instead of seeing the numbers one, two and three, we’ll see zero, one and two. Hopefully you’re able to see why in fact, I’ll just run both.
So if I run this, we see down here zero, one and two. But if I change it to be a pre increment operator, then the value of I is going to be incremented before it prints it out, in which case we’ll see one, two, three. All right, let’s move that back to I plus plus. And what I’m going to do now is change this while to be true. Now, is that legal? Sure it is. Look, there’s no compile errors, no red X’s in eclipse here. So remember that anything that’s in the while loop can be a bullion and a bullion literal, certainly a bullion value. So what do you think would happen if I ran this? You probably made a pretty good guess that it’ll just keep running, right? And you’re right. So you can see it’s just going to keep running because there’s nothing to stop this condition.
So I’m going to hit stop since that’s not what I want to do. But I’m going to keep this true here. I want to show you another way to get out of a loop. And so what I’m going to do is put a check in here and I’m going to say if I is equal to ten, then I want to break. And so we’re starting at zero. We’re going to keep incrementing. Once it gets to ten, then I want to break. And so what the keyword break does is it says whatever loop I’m currently in, I want to break out of it. So this is the loop we’re in, which means that once we hit break, once I is in fact equal to ten, then we continue on after that loop, and it will continue on with the rest of the code. So we’ll run that and it went from zero to nine. The zero is hidden because it scrolled off, but it did say zero.
So that’s another way that we can break out of a loop using the keyword break. Let’s look at the word continue and see how that works. Scroll down here, get another example. So here we’ve got a for loop and what we’re doing is we’re starting with K equal to zero. That’s our initialization section. We’re then checking to see if K is less than 100. And if it is, then do whatever is inside of this four block. After the code in the block has been executed, we’re going to increment K by one. So that should be fairly clear. Next what we’re doing is we’re checking here with an. If we’re saying if K is evenly divisible by seven, then we want to continue. Now what does continue mean? Continue means that we should continue with the loop. We’re not breaking out. But don’t bother doing anything below. Don’t do any of the additional statements that are inside of this for loop. So if K is evenly divisible by seven, it just goes to the next iteration of the loop. Let’s say it’s not evenly divisible by seven. Well then it’s going to print out this message. K is not divisible by seven.
And if I run that you can see that it went through and it printed out all of the different numbers that are not evenly divisible by seven. So that’s continue. Continue says go to the next iteration of a loop. Do not execute any more statements for this current iteration. Now I have one other example for you to look at and it’s going to be a little bit of a challenge for you. What I’d like you to do is to pause the video for a second and take a look at the code in lines 42 through 51 and try to make a guess as to what it’s going to do. So pause the video and come back once you’ve figured it out. All right, let’s take a look and see what it’s going to do. I’m just going to run it and see if it’s doing what you thought it would do. I don’t know if you can really tell, but it is continually going and it’s starting over and over again, going through the numbers zero through nine, over and over again, never stopping. Now why is that? Well, let’s start with line 42. So we’ve got our while and we’re saying while it’s true now, when will that not be true? It’s always true. It’s the bullying true, the literal value.
And so that is always going to evaluate to be true. So it’s going to go into this loop and it’s going to set Z to be zero. And now we’ve got another nested loop which is always set to true as well. So that is checking, it saying, hey, if Z is greater than or equal to ten, then I want you to break. Otherwise let’s print out the value of Z and increment it. So why isn’t it breaking out? Well, in fact it is breaking, but it only breaks out of the current loop. So once Z is equal to ten, then this loop on line 44 is done executing. It continues on at the end of that loop, which is actually still in this other loop. So the loop on line 42 evaluates once again and says, is this true? And the answer is yes. And it resets Z to be zero again. So that’s why it’s constantly reset and why it continues forever. So how would we get out of both loops? Break is clearly not doing it. We have something called a label, and it’s pretty simple to use. What you do is you put some sort of text right above the loop that you want to break out of. So I’m going to say outer underscore, label, and then you put a colon.
And what we’ve done now is we’ve created a label for this loop. And you don’t have to put label. Some people put loop, some people don’t put anything. But by convention, the words are typically separated with an underscore, and by convention, they’re typically all lowercase. I guess I’ve seen both all uppercase and all lower case, but most of the people I work with use all lower case. And then what we need to do is reference that label in our break statement. So I simply say break, outer, label. And now when we run it now it’s just going from zero to nine. Again, the zero scrolled off, but trust me, it was there. And now when we hit Z is greater than or equal to ten, it breaks all the way past the outer label and continues on with the rest of the code.
- Enumerations (enum)
This is the last lecture in this section and we’re going to look at enums. An enum is short for an enumeration, which is more or less a label. And the reason I say more or less is because you can use an enum as simply a label or you can use it as something more robust. Java allows you to create enums with methods and other things as well. Well, so I will show you some examples of that in just a minute. But before I do, I want to show you an example of one of the reasons why we have enums. So take a look at line eight. I’ve got a switch statement and we’re going to switch on a variable called today. Today is just a string for whatever day it is. Now if it’s Monday, we’ll create a string called get to work. And if it’s Friday, we’ll create a string called Time to relax. And otherwise if it’s not Monday, if it’s not Friday, we’ll say Practice Java. So up here on line five, we’ve created that string today and it’s called Friday. So let’s run it. If you look down below in the console, you’ll see that it says Practice Java. Which is good advice, but it’s not the response that I think we were expecting. We were expecting, since it’s Friday, to say Time to Relax. And you’ve already seen what the reason is, right? You found it. It’s a capital F in line five, a lowercase F in line twelve.
And remember, Java is a case sensitive language. So those are really two different strings, even though you and I would just read them as both being Friday. And it’s kind of easy to see the problem here, but a lot of times we’ll be writing code where we’re trying to branch our logic or using some kind of text as an input. And the text could be coming from anywhere, from a user who’s typing it in from a database, from a web service. There’s so many places that the text can come from and there’s a good chance that it won’t match. Now there are ways that we could normalize this. I could have taken it today and run it to all lowercase. I could have done something like this. Today equals today to lowercase. So that would have made sure that they’re all normalized together. But believe me, it can still be a problem even when we’re just writing the variables directly.
So how do we get around this? How do we make sure that Friday is Friday? And one of the ways that we can get around it is with an enum. So I’m going to show you an enum here. This is our days enum you write public enum instead of public class. And then the name, in this case the name is Days. And then in the code block between the curly braces, you just write a comma separated list of all the labels that you want all of the names by convention, we do this all in all capitals. So I’ve got Sunday, Monday, Tuesday and so on. And even though we don’t see it here, each one of these labels has what’s called an ordinal. It’s just an integer value.
Sunday is zero, Monday is one, Tuesday is two and so on. So that’s, that’s a very simple type of an Enum and let’s see how that can fix what we were just working with. So rather than switching on a string, what I’m going to do is I’m going to change this to the days type and watch what happens. If I write days Friday, I get a compiler. It’s telling me right now that there’s no such thing as Friday. So if excuse me, Friday with all lowercase.
So I tried writing with a capital letter. Still a problem. It’s not until I type it exactly with the right spelling, with the right capitalization, that I’m even able to continue. And so that’s one of the nice things. We get compile time checking on our enums. So what I’m going to do now is I’m going to switch on that today. So my case can no longer be the string, it’s got to be an enum. So I’ll put Monday here and Friday here and now when we run it now it says time to relax, which is what we had expected. One thing to note is that when I’m working with the enum just in this class, I do need to put the type. So I have to say days Friday.
But inside of the switch you just put the value and if you forget, well, the compiler will tell you if I put Days Monday, it’s just not going to allow that. So you simply put the value. Now let’s look at a more complex enum. And so here is my music type enum and I’ve got all of my labels just like I had before, but in parentheses I have a number in here. And what’s really happening is when I have this parentheses, it’s calling a constructor.
So an enum can have a constructor. So here’s my constructor, I’ve got my music type constructor that takes an into and we’re just putting an ear damage factor and we’re storing that ear damage factor as a variable, as some state inside of this enum. And we can even provide methods. We can do a calculate the hearing loss pass in some hours and it will tell us what the ear damage factor is multiplied by the hours so that we know what our hearing loss is going to be. You can even have a two string. So note that there’s some cool things you can do with an Enum. We can say what is the name that will give a string representation of the name.
So if it was jazz, it would print out a string of jazz. We’ve got the ordinal, which is the number that’s backing it up and don’t be confused by ordinal and what you’re seeing up here. One, four, six and eleven. One, four, six and eleven are not ordinals. That’s just the number that’s being passed into the constructor. Classical is still zero, jazz is one, rock is two and metal is three. Now, why do you care about the ordinal? Later we’ll talk about working with an array and one of the things that you can do is get a music type array. It’s just a collection of all the different values you can pass in the ordinal. Like I could say, hey, music type array, I want whatever’s in the first indexed element and that would be jazz. It’s jazz and not classical because classical is zero and jazz is one. So if we pass them to number one, then we would get jazz.
Now, that’s a little abstract for you at this point because we haven’t talked about arrays in any detail, but we will be looking at them in a future section. One last thing that we can do here to show you. So again, if I want to convert one of these words to a string a day, I can say days Friday, and then I can just say name and that will return a string representation of Friday. It’ll just be Friday in all capital letters. I can also create a strongly typed music type. Let’s do day. Let’s stay with the same example. So I’ll say days. Let’s say I want to create Tuesday. So I’m just going to say days. It’s going to be Tuesday. So sure, I could say days Tuesday, that’s the literal way to do it.
But I could also take some input to see what day they want to pass in. So in fact, let’s just make this the user’s day, whatever it is that the user want to give us. And so the user passes in some kind of a day and let’s say they pass in Tuesday. So what we can do to turn a string into an enum value type is we would say days value of and then in the quotes you would just pass in that string and then that gives us a strongly type days enum. So that’s it for enemas. Enums are used as labels, although they can have methods as well. And they’re really used to enable us to do strong typing that we can check at compile time to see if we’ve done anything silly.
- Java Syntax Lab
Lab number six is a big lab and it might take you a while to finish it. When I teach the class and have students in the classroom, it often takes about an hour to finish this lab. So with this lab you’re going to be working with, if statements switch statements, you’re going to be doing loops. So you’re going to be exercising a lot of the syntax that we just learned. All of the instructions are in the PDF that’s attached to this lecture. In the resources section, it’s lab six syntax PDF. Download that. Have at it and let me know if you have any questions. Oh, and of course, don’t forget the solution code is in the lab. Just go to the back of the lab if you get really stuck.