Java Fundamentals

Operators

Angie Jones

Angie Jones

Java Champion
Java Fundamentals

Check out a free preview of the full Java Fundamentals course

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

Angie discusses relational operators in Java, including > greater than, < less than, greater than or equal to >=, less than or equal to <=, equal to ==, and not equal to !=. The logical operators && AND, || OR, and ! NOT are also covered in this segment.

Preview
Close

Transcript from the "Operators" Lesson

[00:00:00]
>> Okay, so we've been using a lot of conditions within our decision structures and with those, we're using expressions. So I wanted to quickly go over some relational operators. So these are the symbols that we've been using within our conditions. Java has six relational operators. So you can use greater than, less than, greater than or equal to, less than or equal to, equal to, and not equal to.

[00:00:32]
So the equal and the not equal, we haven't used their conditions yet. That equal operator, so this is the equality operator, you have to be very careful in Java. If you use a single equal sign, that is for assignment. So even if you said if this single equal sign that, what that's gonna do is assign that value to that other value and it'll be true, right?

[00:01:02]
And so you'll go inside of that condition all the time, you'll be scratching your brain, this doesn't equal this, why is it going inside of this if statement? It's because you just assigned something, right? So you have to use the double equal sign. That checks for equality, right.

[00:01:19]
Logical operators, so these are great. If you wanna combine two conditions into one, right? So there are three of these, one is the AND operator. This will look at two conditions to determine if, collectively, this whole thing is true. In order for it to be true, both of the conditions must be true.

[00:01:45]
So in this example, it says, is 1 less than or equal to 2? That's true. And is 4 not equal to 5? Also true, they both need to be true. If the first one was false, it would not evaluate the second one because there's no need. It won't result to true because they both have to be true.

[00:02:08]
So that's called short circuiting. In the second one we have OR. This is two pipes. At least one condition must be true in order for this to be true. So in the example we have is 3 equal to 4, that's false. But only one needs to be true, so it is gonna check the second one.

[00:02:29]
Is 2 equal to 2, that's true. Only one needs to be true, this whole thing is true, all right? If the first one was true, it would not evaluate the second one because we only need one to be true. And then the last one is not. This one is going to take whatever the value of the condition is and negate it.

[00:02:53]
So in our example it says there's 2 equals 3, that's false, but we have a not so the whole condition becomes true. Okay, questions on logical operators. So, remember we looked at the nested IF statements and we did, if salary is greater than or equal to requireSalary, then we went in we asked another question if the years is greater than or equal to requireYearsEmployed.

[00:03:23]
We could simplify this by using the compound conditions and use a logical operator instead. So, for example, we can say if the salary is >= the requiredSalary && the years are >= the required loan. So we're putting both of those conditions into one, okay? When I said about the short circuit, so notice the sentence that I have on the else, sorry, you do not qualify.

[00:03:55]
I can't tell you whether you don't qualify because you didn't meet the salary expectations or because of the require years. I don't know which one of those failed, right? So if it failed on the first one, it would have just went right into the else, and not even looked at the years.

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