1z0-808 Oracle Java SE 8 Programmer – Java Syntax
- Section Overview
This next section is going to focus on Java syntax. So all the things that we’re going to use when we build classes and use objects, we’re going to talk about conditionals for branching, our logic. We’re going to talk about loops for iterating through logic. We’ll talk about some other constructs such as enums. And then we’ll look at all the different operators that are available to us.
- Condtionals – If / Else If / Else
All right, we’re going to start by looking at conditionals. In this lecture, we’re going to look at the if statement. So let’s start with that. What is an if statement? An if statement is where we can run a test, and if the test is true, we’ll do something. And if the test is not true, if it’s false, then we won’t. So let’s look at a very simple if statement. First of all, in order to work with a conditional, we’re going to need a Boolean value. So I’m going to make a Boolean variable. So boolean in Minneapolis equals false. And now we’re going to write our if statement. So I say if, and then in parentheses right next to it, we’ll write our test. And so my test is going to be in Minneapolis. Now, this test can be any Boolean value, so it can be a Boolean variable. It could be just a literal like true. It could be a method that returns a Boolean variable, and it also can be a Boolean operator.
So if we had, for example, greater than or equal to if my age is greater than or equal to 21, then do something. And if it’s not, then don’t do it. That kind of a thing. So greater than, equal to, there would be a bullion operator. And we have a whole lecture on operators coming up, so we’ll look at those in greater detail, just a little bit. But for right now, we’ve got our test if in Minneapolis, then we’re going to do something. So we put some curly braces.
We have a code block. Notice the errors just went away. And so what we’re saying is, if this is true, if we are in Minneapolis, let’s do something. And we’ll just print out a little message. We’ll say, hello from Minneapolis. All right? That’s it. That’s a simple condition. And we use conditionals in code to branch our logic so that we can make decisions at runtime to say, should we do something or shouldn’t we? So let’s actually run this and see what happens. So if I run it, I’m not going to expect too much.
Let’s run that. Nothing. There’s nothing down here in the console. Why not? Well, in Minneapolis right now is set to false. So when it does, this test is in Minneapolis true? The answer is no, it’s false. What would happen if we changed it to true? Well, probably exactly what you would expect. It now says, hello from Minneapolis. Okay, well, let’s change it back to false. What are some of our other options? Another thing we could do is we could then check to see if we’re in another city. So let’s add another variable here boolean in St. Paul. So now let’s check that, and we do that by saying else if. And we have some parentheses, and inside the parentheses, we add our test. So in St. Paul. All right, if we’re in St. Paul now, we’ll say hello from St. Paul.
So let’s take a look at the code in more detail and then we’ll run it. What’s going to happen? First of all, it’s going to check to say are we in Minneapolis? And the answer is no, false. So then it goes to the next statement here else if in St. Paul and the answer is true, in which case it will enter this block and execute that code. Let’s run it to make sure. And there it is. It says hello from St. Paul. Now the rule is once a statement is true, once one of these conditional checks evaluates to be true, it enters that code block and then it exits below. So if in Minneapolis happen to be true, it wouldn’t even check to see if we’re in St. Paul. It would run this line eight. It would say hello from Minneapolis and then it would continue on with line twelve. So you only enter one of the blocks of an if statement. Now we could have as many of these else ifs if we want. Let’s turn this to false. So now none of these are going to be true. What else could we do? Well, we can have an else. And an else is sort of a catch all. If nothing else above these statements evaluated to be true, then let’s just say hello from somewhere and let’s run it. And that’s what it says, hello from somewhere.
Because both in Minneapolis and in St. Paul were false. None of these were true. If I turn in Minneapolis to be true, it will only print out hello from Minneapolis. None of the other code blocks would be entered. So that’s the basics of the if statement. You can have an if alone, you can have an if with an else, you can have an if with as many else Ifs as you’d like, and you can have an if with an else if as many elseiffs as you like, and an else, you can’t have an else if just floating in space. You can’t just have an else. There always has to be the if statement. Now the parentheses that we’re seeing here, these are required. Your test has to be in parentheses. What’s not required are these curly braces. I’m just going to get rid of them just to show you.
So we’ll just get rid of that and we’ll get rid of that. And as you can see, that is legal, but it’s not always going to do what you expect. Now let me show you what I mean. Let’s add another statement here. So we’ve got this in Minneapolis, in St. Paul else system out print line hello from somewhere and let’s just save it and run it, make sure it’s still doing what we expect. And I’m going to set both of these to false. So what I would expect is it’s going to say hello from somewhere. And that’s what it did. It said hello from somewhere. Now, what I’m going to do is change one of these to be true. Let’s just grab is St. Paul, test it again. You should know what’s going to happen next. They’ll say, hello from St. Paul. It’s looking good. Okay, so let’s put another statement here. Let’s say that if it’s not if we’re going to say hello from somewhere, we’ll also say system out print line.
Can we visit you in St. Paul? All right. So if we get to this hello from somewhere. I want to print out this message. Can we visit you in St. Paul? Once again, let’s put false false. We’ll run it and there it says exactly what we expect. Hello from somewhere. Can we visit you in St. Paul? Let’s put that St. Paul to true again. What do you think is going to happen? Let’s run it. Hello from St. Paul. Can we visit you in St. Paul? Now, that’s weird. Why did this line run? And the answer is that if you don’t put the curly braces, then the first statement that follows is associated with the conditional. But the second statement is not. It’s just there. It’s just a statement.
So this is one of the reasons why it’s really good practice to make sure you do put in your curly braces. I’m going to just clean this up a bit and let’s just take a look. Now that I’ve got the curly braces, if I run it now, it does what we’d expect. Hello from St. Paul. Because the curly braces allow us to put in multiple statements within a single block. So those are the basics of the if else if and else statement.
- Conditionals – Switch
We’re going to look at a different kind of a conditional statement. Now it’s called a switch statement and the syntax is a little weird. So let’s jump right into it. First of all, we got on line eight here the word switch. And then in parentheses we’re checking something, something called J. We’re checking to see what is the value of J. So the parentheses here is they’re different than what we are seeing in the if statement. With an if statement. The parentheses was a conditional check. It was a test. Anything that was going in the parentheses had to evaluate to a boolean, which meant it could be a boolean. Literal, it could have been a variable that was a boolean. It could have been a method that returns a boolean. That’s not what we’re doing here.
We are just checking to see what is the value of J. And then we’ve got these curly braces on line eight and 23. And so that’s our code block. And we’re saying based on whatever J is, let’s run these tests. So the tests are really inside of the curly braces. So the test is really going to be a case. And so we write the word case and then we say what do we think it might be? So case one, colon. And what that means is if J is one, then enter here and run this code. String equals one and break. Now what does break mean? Break means that’s it. Once you’ve finished here, don’t check any of the other cases. Just continue on after this block, which would be with line 24. Well, let’s run this. So I’m just going to run right now. J is equal to three and I’m going to run it. If you look down below, you’ll see that it prints out the string three. So int J equals three.
We switch on j So it says what is J? J is three. Is it case one? No. Is the case two? No. Is the case three? Yes. And so we enter and our string becomes the word three. We break on line 17, which brings us all the way to the end of the code statement. And so then we enter line 24 where we print out the string and that’s why it says three. If I change this to some other number that isn’t here, like zero, and run it, you’ll see that it prints out our default, which is unknown. Now what would happen if we forgot a break? Let’s do that with case two. I’m going to get rid of the break here and I’m going to set my value to be two. And so what will happen? Well, it prints three. Why did it do that? Well, what happened was we switched the value of J was two and it did enter case two and it did set the string to be two, but there was no break.
So it continued on even though the case three is not true. It ignores that and so it falls through to the next case. And string became three and then we hit the break. So put that break back. And that’s the basics of a switch statement. Now the J that we’re testing on right here, what can we put in a switch? We’re actually fairly limited into what we can put here. The first thing is that it could be anything that is an int or can be converted into an int implicitly. And so if you remember we saw that we can do widening conversions without having to explicitly cast a number. So that means we can switch on an int. We could switch on a short notice. There’s no compile error there. We could switch on a byte, no compile error. We can even switch on a char.
Remember that it’s legal to use numbers for our characters. So all of those are legal. How about what’s legal for each case? So for each case we have to have something that’s known at compile time, which is usually going to be a literal, some literal value that we use here. We couldn’t have a variable for a case, we couldn’t call a method for a case. It has to be known at compile time. So let’s look at a few other examples. Scroll down here. Here’s an example of using a char. This time we’re using an actual literal character. So if it’s A, we’re switching on that character. Here’s our case. If it’s A, let’s print out it’s A, otherwise we’ll print out the character. Run that and it does say is A. Another thing that we can switch on is a string. And this is new to Java seven. This didn’t used to be available. So now we can have a string switch on it. And there’s our case has to be a string literal because the value has to be known excuse me at compile time.
So what’s it going to do? It’s a M should print out good morning. And it does. Now let’s look at one final example where we purposely leave out some of those breaks and we do that so that we can have a couple of different conditions that could be true. So now we’ve got J is equal to three and we’re going to switch on J. And if it’s case one or case three, then we want it to print out odd. And if it’s case two or four the string should be even and so on. All right, I’m going to print out my print out otherwise it will not print it out and we’ll print out string. So let’s run this. And it says odd. So what happened was it was three, case one wasn’t true, but case three was. There was no break. So both of these are considered part of the same test really. And we entered here and string became odd. What if I had to change it to two? And now it says even so, that’s an alternative syntax for issuing a conditional.
- Operators: Part 1
What you’re looking at here is a table that shows the order of precedence for Java operators. We’re going to use operators in expressions all over the place. So it’s really important that you know these and especially if you’re taking the exam, you really want to know not only what the operators do, but what the order of precedence is. So what do I mean by order of precedence? It’s what is going to be executed first. So let me show you an example. So if I’ve got an equation here where I say int x is equal to three plus two times five. So the question is what is x going to be after this is done? Well, because of the order of precedence, you don’t just read this left to right. What you have to do is know which of the operators have a higher order of precedence. And so multiply has a higher precedence than addition. So the five times two would be executed first and then the plus three would be executed next. So if we run this, I would expect it to say 13. Let’s run that real quick. And there it is, 13. So the higher level the row, the higher the order precedents. And then within any given row these are all equal. And so you would simply read them left to right in the equation.
In other words, if I have plus and minus, neither of those have precedence over the other. We would just keep moving left to right in the equation to see how the equation should be evaluated. So let’s go over some of these different operators. We have boolean operators. These return true or false reading all the way from left to right. We’ve got less than, greater than, less than or equal to, greater than or equal to. Sometimes people call this the bang operator or not. This is not equal to and this is equivalence. So it’s equal to. If you have a single equals, that’s an assignment. Two equals means we’re checking for equivalency. Now let’s talk about bit shifting and bitwise operators. And I’m going to go to code just because it’s a little easier to see it there. So don’t worry, you’re probably not going to have to learn binary. But I’m going to show you the binary representations here because it makes these operators a little easier to understand.
So here I’ve got int I equals eight and the binary representation is listed right over here. When we have the bit shifting operator and we have three of them, what it’s going to do is do exactly what it says. It’s going to shift the bits. So we’re taking the value of I here and we are shifting it over one place and we’re shifting it to the right. That’s what the two arrows means. So here we’re taking that one, shifting it over. And so once we’re done, this is our new number, which would be the number four. Now that is a signed shift. When you have the three arrows, that’s what’s called an unsigned shift. So what it means to be signed is that the range of numbers can be from the negative to the positive. And unsigned means it’s just a positive number. So the way that we are able to tell if a number is negative or not is whether the most significant bit, which is the one all the way to the left, if it’s a zero or one. If it’s a zero, it’s a positive number. If it’s a one, like we have down here, then it’s a negative number. So when we do assigned shift, then what it does is it respects whatever that most significant bit is and it keeps repeating it as it does its shift.
So in this case, when we shifted over one place, the most significant bit was a zero. And so it just kept adding zeros on the left side. When we do the unsigned shift here, then what it’s going to do is even if this was a one, it would fill it in with zeros. So it’s unsigned, every number is going to be positive and then we have the left shift. At this point, after line eight had executed, the number was one. And when we shifted it over three, now we’re back to eight. So now let’s look at the negative number example. We’ve got a negative number eight. Here’s the binary representation. Notice that the most significant bit has a one in it.
So now we’re going to shift it over to the right one place. Notice I’m using a slightly different operator here. This is an assignment operator, it’s a shorthand. So this operator means take X, shift it to the right one and then assign that new value back to X. It’s just a shorthand. So what happens when we shift it over to the one? Well, it is a signed shift, so it fills in the left side with more ones. In this case, it filled it in with a single one. And you can see that everything moved over to the right by one place. This is an unsigned shift right here. So when it fills it in with two more digits, it’s going to fill it in with a zero because the zero makes it a positive number. Now, let’s take a look at the bitwise operations. So here we’ve got two numbers, two INTs, we’ve got the number eight, the number ten, and here’s their binary representation.
When you see the single and here, it’s a bit wise. And what it’s going to do is it’s going to look at the two numbers and if both of the digits in that place are a one, then it will have the number one, otherwise it’ll be a zero. So when we do that calculation, we get 10001 because here in this place, both of the digits were one, then they were zero. The third one has a zero and a one. That doesn’t count either. They both have to be a one in order for it to result in a one and the final is a zero. So that gives us a thousand or not 1000, it gives us 1000 and then we’ve got or which is going to go through the same two numbers and it’s going to say if either one of these digits is a one then the result is a one. So first place would be a 1, second would be a zero. The third we’ve got a one here so that counts. The third would be a one and the fourth would be a zero.
So we’re left with 1010 exclusive or is kind of like or except it says that only one side has the one in order to result in a one. So even though there are two ones here with the exclusive ore that’s going to result in a zero, second would be a zero, third would be a one and the fourth would be a zero. And that’s what we got right down here. And finally this tilde is called the complement operator. It just flips all the bits. So here’s all the bits for X and you can see down below we just flipped them. If it was a zero it becomes a one. If it was a one it becomes a zero. And the last operator we’re going to talk about in this lecture, we’re going to continue with operators in the next lecture but for this particular one let’s look at booleans. So I’m going to create two methods.
Here I’ve got Is Sleeping and it’ll print out the words Is Sleeping so you know that the method was called and it’s going to return false and Is Home is going to print out that it’s calling Is Home and return true. Now here’s my first boolean example and we’re using the bitwise and here it’s treated as a boolean operator in this case and we could use the or as well, I’ll show you that in a minute. But here we have and what happens is we’re saying if this side is true, the left side and the right side is true, then do whatever we’re going to do in this boolean operation. So let’s run it. And when we run it we can see down below here that it says Is sleeping because that was called here and it enters this method, prints out Is Sleeping, returns false and then calls Is Home and returns true. It evaluates both of these and it says well this is false, this is true so they’re not both true.
Therefore we won’t enter this if block. If we change this to an or it calls both methods and now it does give us the response we are looking for because this is saying if the left side is true or the right side is true. But typically when we’re making boolean methods we don’t use the bitwise operators. Instead. You’ve probably seen this where we have two of the ands and two of the ors. And the difference is that this is short circuiting. Let me demonstrate. I’ll hit the play. Notice that it says is sleeping. It did not even call is home. It’s short circuiting because as soon as it knows the answer it doesn’t evaluate the other side. So in this case we’re saying that both sides have to be true when it evaluated is sleeping, that returned false. And so it said well, this can’t possibly be true. So I’m not even going to call the right side if we did an or.
Now it had to call both because the is sleeping was false. So it still had to check the other side to see if his home was true. If we change this, if I instead do is sleeping and we run this notice that it never called is sleeping, it didn’t need to. It short circuited because it already knew the answer. And one of the places that you’re going to find short circuiting used is when we’re trying to prevent something from running if it’ll cause a problem. Very common is checking for null. So let me show you in a quick example of that and we’ll finish up with this lecture.
So what I’m going to do now is just say string x equals null and I’m going to just trim x. So I’m going to say x trim and let’s run it. And down below you can see that I’ve got a null pointer exception. The reason is that any time we try to invoke a method or read a variable, anytime we’re using the dot operator on a null reference, it’s saying there’s no object for me to refer to.
So it throws the null pointer exception. So we can use the short circuiting operator to our advantage. We can do things like if x is not equal to null and x trim length is greater than zero. It’s kind of a common thing that we do. So here we’re checking to see making sure it’s not null, getting rid of the white space after we get rid of the white space before and after the string, we’re checking the length to make sure it’s greater than zero. So we’re basically saying it’s not an empty string, then it will execute this code. So now if I run this notice the null pointer goes away because we’re checking to make sure it’s not null. And what’s happening is this is going to fail. It’s going to say but x is null. So it’s not even going bother to evaluate the right side.