Logical Operators

Learn the Basic Java Concepts Working with variables and operators
10 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.24
List Price:  €93.20
You save:  €27.96
£55.93
List Price:  £79.91
You save:  £23.97
CA$95.58
List Price:  CA$136.56
You save:  CA$40.97
A$107.13
List Price:  A$153.05
You save:  A$45.92
S$95.25
List Price:  S$136.08
You save:  S$40.82
HK$547.90
List Price:  HK$782.75
You save:  HK$234.85
CHF 63.85
List Price:  CHF 91.21
You save:  CHF 27.36
NOK kr770.13
List Price:  NOK kr1,100.23
You save:  NOK kr330.10
DKK kr486.51
List Price:  DKK kr695.05
You save:  DKK kr208.53
NZ$117.65
List Price:  NZ$168.07
You save:  NZ$50.42
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,679.19
List Price:  ৳10,970.75
You save:  ৳3,291.55
₹5,834.32
List Price:  ₹8,335.10
You save:  ₹2,500.78
RM333.81
List Price:  RM476.90
You save:  RM143.08
₦91,422.33
List Price:  ₦130,608.93
You save:  ₦39,186.60
₨19,487.31
List Price:  ₨27,840.21
You save:  ₨8,352.89
฿2,585.90
List Price:  ฿3,694.31
You save:  ฿1,108.40
₺2,277.50
List Price:  ₺3,253.72
You save:  ₺976.21
B$361.10
List Price:  B$515.88
You save:  B$154.78
R1,320.17
List Price:  R1,886.05
You save:  R565.87
Лв127.48
List Price:  Лв182.13
You save:  Лв54.64
₩96,293.43
List Price:  ₩137,567.94
You save:  ₩41,274.51
₪266.53
List Price:  ₪380.78
You save:  ₪114.24
₱4,038.21
List Price:  ₱5,769.12
You save:  ₱1,730.90
¥10,975.59
List Price:  ¥15,680.08
You save:  ¥4,704.49
MX$1,206.90
List Price:  MX$1,724.22
You save:  MX$517.31
QR255.26
List Price:  QR364.68
You save:  QR109.41
P965.80
List Price:  P1,379.77
You save:  P413.97
KSh9,448.65
List Price:  KSh13,498.65
You save:  KSh4,050
E£3,352.63
List Price:  E£4,789.68
You save:  E£1,437.04
ብር3,985.10
List Price:  ብር5,693.24
You save:  ብር1,708.14
Kz58,489.70
List Price:  Kz83,560.30
You save:  Kz25,070.60
CLP$66,385.51
List Price:  CLP$94,840.51
You save:  CLP$28,455
CN¥507.19
List Price:  CN¥724.59
You save:  CN¥217.40
RD$4,111.01
List Price:  RD$5,873.12
You save:  RD$1,762.11
DA9,398.61
List Price:  DA13,427.17
You save:  DA4,028.55
FJ$158.17
List Price:  FJ$225.97
You save:  FJ$67.79
Q544.39
List Price:  Q777.73
You save:  Q233.34
GY$14,639.41
List Price:  GY$20,914.34
You save:  GY$6,274.92
ISK kr9,791.60
List Price:  ISK kr13,988.60
You save:  ISK kr4,197
DH708.11
List Price:  DH1,011.63
You save:  DH303.52
L1,243.72
List Price:  L1,776.81
You save:  L533.09
ден4,016.98
List Price:  ден5,738.79
You save:  ден1,721.81
MOP$563.94
List Price:  MOP$805.67
You save:  MOP$241.72
N$1,340.22
List Price:  N$1,914.68
You save:  N$574.46
C$2,575.16
List Price:  C$3,678.96
You save:  C$1,103.79
रु9,316.74
List Price:  रु13,310.19
You save:  रु3,993.45
S/261.07
List Price:  S/372.97
You save:  S/111.90
K269.52
List Price:  K385.04
You save:  K115.52
SAR262.50
List Price:  SAR375.02
You save:  SAR112.51
ZK1,842.03
List Price:  ZK2,631.59
You save:  ZK789.55
L324.63
List Price:  L463.78
You save:  L139.14
Kč1,640.70
List Price:  Kč2,343.96
You save:  Kč703.26
Ft25,605.88
List Price:  Ft36,581.40
You save:  Ft10,975.51
SEK kr762.91
List Price:  SEK kr1,089.92
You save:  SEK kr327.01
ARS$61,153.48
List Price:  ARS$87,365.86
You save:  ARS$26,212.38
Bs484.56
List Price:  Bs692.26
You save:  Bs207.70
COP$275,828.59
List Price:  COP$394,057.74
You save:  COP$118,229.14
₡35,172.98
List Price:  ₡50,249.28
You save:  ₡15,076.29
L1,727.65
List Price:  L2,468.18
You save:  L740.53
₲519,796.34
List Price:  ₲742,598.03
You save:  ₲222,801.68
$U2,684.12
List Price:  $U3,834.62
You save:  $U1,150.50
zł281.95
List Price:  zł402.81
You save:  zł120.85
Already have an account? Log In

Transcript

Hello there, and welcome back to the Java development course. So last time we were doing the relational operators, and you had some homework to do, your job was to create or to extend the application that we created previously, and add in more than less than more than or equal to and less than or equal to functionality as well. So here is essentially what you should have done. So here we have Boolean more than is a is more than B, than Boolean less than is a is less than b, Boolean more than or equal to is a is more than or equal to b. and less than or equal to is a is less than or equal to b. And then I just print the values correctly. And this is essentially the whole application.

Let's go and run. Let's go and run this and we can no input. Let's say seven and eight. And there we go. Seven is equal to eight is false, seven is not equal to eight is true. Seven is a little more than eight is false, seven is less than eight is true, seven is more than or equal to eight is false, and then seven is less than or equal to eight is true.

Let's go to input to have the same numbers. And there we go seven equal to seven is true seven, inadequate, seven is false, seven is more than seven, false false, seven is more than or equal to seven is true. And then seven is less than or equal to seven is also true. So there we go. That is essentially what you should have done. So if you've got that, that's actually very good.

If not, don't worry about it, try understand this code, replicate it, add some things to it, you know, get creative, and you're good to go. So without further ado, let's get into this lesson. Alright, so today, we're going to be going over logical operators. Now, I'm sure at this point, you're looking at me like how many operators are there. And there are actually quite a few, but the point is, we're going to be using all of them later on. So it may not seem all too important as of now, but later on as we start to get into more complex Applications and start to really, you know, discover new features in Java, you'll start to realize just how important and useful these operators can be.

So let's go and create our class for today. This is going to be called logical logical operators. There we go. A public static void Main as always finish. Alright, there we go. Okay, so inside.

So logical operators, how do they work? So logical operators are mainly used on Boolean. So let's say that we have to boyens Boolean, t equals true and then Boolean. F equals false. There we go. Okay, I'm pretty sure there's actually only two logical operators.

We have the and then and then or And then not there we go. Okay, so that is essentially the three operators, I think No, not two operators, three operators. Anyway, those are the three operators of logical operators. And it usually work. So the end end works a little bit like this. So if we have to Boolean so it's gonna, I'm gonna actually go to do System dot out dot print.

Okay, and so now we can do t, and F. So what will this do? Well, it will check if t is true. And then if f is true, and if both of them are true, then it'll return true. If either of them is false, then it'll return false. So in this case, what is it going to return? Well, we have true and false.

It's going to be false. Let's run this. There we go. We get false. Okay, so that is the answer. Operator, we also have the OR operator as well.

So the OR operator works a little bit like the AND operator, and ORS just to sticks to these little sticks stick thingies. So the OR operator works a little bit differently than the end operator, but it's the concept is actually the same. So two Boolean true or false or you know, whatever they are. And if either of them is true, then it will return true. So in this case, T is true, and f is false. So one of them is true.

So it will return true. So, let's run this. And there we go. Sure enough, it returns true. Okay. And finally, the knot operators.

The NOT operator just inverts the value that we have So it goes before a Boolean. There we go. And so this will print. False. There we go. So the no operator just you know, takes a Boolean flips it so if it's true becomes false and if it's false, it becomes true.

And then print the result won't wait, it doesn't print the result. It just, you know, just flips it. There we go. Okay, so now if we run this, we get true. There we go. So you can see it flips.

And so operators can actually be combined. So we can have flipped false and true. or, or, or, or, which say, true. There we go. And let's run this. And we get true.

Since in this case, we have false and and true which is false, and then that's flipped by this exclamation point here. That's true or False true is flips as false, true or false, and we return true. And we print true. So there we go. So that is essentially how we work with logical operators. Okay, so it's pretty simple, nothing too difficult.

Let's create an application just like we did with the other operators very quickly using the console to get to Booleans. And then, you know, or them, and so on. Okay, so let me go and actually comment this out. There we go. That's a handy feature. If you do command slash, it'll comment whatever you have highlighted.

Okay, so let's do this. We're going to create a new scanner as always scanner at C equals new scanner. There we go. Alright, so now let's go ahead and import it as always as well. And system.in There we go. Alright, so now we can go ahead and do it.

We can go ahead and do Boolean. First equals SC dot next Boolean. Boolean, second equals SC dot, next Boolean. There we go. And so now we can do a Boolean, and ID equals first. And, and second.

There we go. All right. And so now we can do System dot out dot print ln. And then we do first plus and and plus second plus equals plus ended. All right, there we go. Let's run this import.

Let's just say import true and False, whoops, false. There we go true and and false equals false since we have a false here, so in result, it prints false. Now, give me an idea of exactly what happens when we input something, not Boolean, it just gives you an input mismatch exception. So in this case, it's trying to get a Boolean, but we just put t when it was expecting true or false. And so in result, we get this exception, so as an error, so that is essentially how we work with logical operators. Now, let's assign you some homework.

Alright, so for homework, it's also going to be pretty trivial, just like we've been doing for the other operators. We actually, I believe, have one more type of operators to do, or do we not when we think about this? Yeah, yeah, we actually do have one more type of operators, the assignment operators to do. So it's this and then the assignment operators and then we're done with operator, so on Operators are a little bit boring, but they are crucial. And they are very important to know. And I don't expect you to remember all of them right now.

But then as we're, you know, getting into lessons as we're going to be doing this, you're going to start to understand them, you're going to start to see where they're useful, you're gonna start to use them. And you'll see how you can create pretty complex applications using different types of operators. Anyway, back to the lesson here. So for homework, your job is to add functionality for or as well as for the not so invert. So you know, the exclamation mark. So pretty simple.

Exactly. Just like we'd be doing before. shouldn't take you too long. It shouldn't be too hard. And yeah, I wish you luck. And I'll see you next time.

Doesn't Yeah, hello. This is me from the future here. I could believe forgot to mention, since there's really no way for us to invert the first you know, in To use the invert operator it can only be used on one Boolean. So for for the inverted operator you're going to add to them so first and and second and then invert that. So just that just so you can get an idea of exactly what to do. No confusion.

There, there will be no confusion. So yeah, I wish you luck and I'll see you next time.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.