Assignment Operators

Learn the Basic Java Concepts Working with variables and operators
9 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 went over logical operators, you had some homework to do your job was to create extend the application that we wrote previously to add support for or and inverted statements as well. So here essentially we should have done, okay, so here I just have an order Boolean, which is first or second, and then an inverted a Boolean, which is first and, and second. And that's inverted. So pretty simple, you know, nothing too complex. We already went over all of this in the last lesson.

So let's go and run it. Test it out. The real so true. False, there we go for true and and false is false. True or false is true. True and and false inverted is true.

So pretty simple. Let's go and actually test it out one last time, just true and true. And there we go. So true, and then true, is true, true. True or true is true, true and true inverted is false. So there we go.

So that essentially how we work with logical operators. So if you've got that very good, if not, again, try to understand it, and you're good to go for the rest of the course. So other than that, then let's get into the lesson. Alright, so today we're going to be going over assignment operators. And I promise this is the last lesson with operators. They can get quite boring.

I know and I'm sure at this point, you're like, what is the point of this is so boring, there's nothing to do? Don't worry. This is the last lesson after that we're done with operators, we can move on to more interesting things. Luckily, so let's go in and create a new class. assignment operator assignment operators, sign sign meant operators. There we go.

Seven operators actually very useful. And we've already used them quite a bit before. And there's a lot of things you can do with that assignment operator. So here they are. So let me go What actually do them? Write them out?

Equals plus equals minus equals times equals divided by equals. Let me see. So I think there's also a modulus equals or is they're not let me go check specifically. Okay, I checked specifically and there was one. Okay, so there we go. So these are the assignment operators.

So what do the assignment operators do? Well, first of all, let's go over the very simplest one, this equals operator. So let's say that we have an integer a, right, we now need to assign the value some value to a, so some number, we do that using the first operator A equals nine. There we go. All right, so very simple operator, just a equals nine. So now a is going to be equal to nine.

So nine is going to a, his value is going to be nine, and so on and so on. Now, what if we want to add one to a well, we would have to do a equals eight. plus one, right? That's gonna be 10. But we use a lot of code. What if we want to add 10 just just just for, for reference, we will have to do that.

But again, that's a lot of code, we can just do plus equals, there we go. So a plus equals 10 will add 10 to a, so 10 to nice, that's 19 and then put the value into a, so it will now be equal to 19. A is 19. There we go. Let's print it. System dot out dot print ln A.

There we go, and 19 as predicted. Okay, so other operators, we have the minus operator. So minus equals, this is all pretty self explanatory. Actually, no, I got rid of the Sit down. There we go. So a minus equals 10.

We'll subtract 10 from a and then put the result back into a let's run this negative one. As expected, we also have times equal 10, which is going to multiply a by 10, and then put the result back into a. There we go 90 again, as expected. And we could also do divided by let's make a 100 for this, just for kicks. And there we go. So 10 hundred divided by 10 is 10.

And then finally, we have the modulus operator as well. So we can go ahead and do modulus operator whoops, nope, minus operator. Let's make it 100 modulus 33. And this game is going to be one since 33 goes into 103 times with the remainder of ones that's 99. And there we go. So that is operators and that is how operators work.

Now, a couple things we need to clear up. So first of all, order of operations. I'm sure at this point, you're wondering about the order of operations. So let's say we have int a equals 12. And then int B equals a times 12 divided by three plus 14. I don't know maybe times five, right?

So order of operations do make a difference in this case. So if we go ahead and actually run this, we get actually no hi didn't print anything. system out print ln B, there we go. If we run this, we get 86. Right? But that's the thing.

There are order of operations here. So if And for that, we have parentheses. So we put parentheses here, and then I think, there to do it would be also here. That will change it. There we go. Yeah, as you can see, now it adds 12 to a, and then actually does the rest of the equation.

So order of operations are essentially in Java, just like in math. So first does multiplication and division, then addition and subtraction and so on. And so parentheses, we do have parentheses so we can put things into parentheses. And so then we, you know, can control the order of operations. If we don't close the parentheses we have they have to be closed. So if you don't close it, we get an error like this.

We can proceed actually, it will give us exception in thread main. So the problem is the, this thing here to answer that to complete it, and then that's, that will complete. Okay, so that is one of the things cleared up. Second of all, all of the operators that we went over that are for numbers, work with doubles and floats as well, so where they work with all numeric data types, so if we have a double b equals 3.4, we can now do System dot out dot print ln B times 45 divided by four for example, and we get 38 point 25. So just as an example, we can also do that as well. And so the same thing goes for float.

So if you have a float here, we can also do all mathematical operations on it. If we have a long here as well, we can do mathematical operations on long as well. There we go, and so on and so on. So with all data types, all numeric data types, we can perform operators on those data types. Okay, and so that is essentially how operators work in Java. All right, so, homework, this is going to be a little bit unusual homework.

Let me go and actually create the homework class really quickly. Create a homework class, the operators home, work, there we go. Okay, so for homework, what you're going to do is you're going to utilize the scanner class, as always, so scanner, S equals new scanner system.in. So you're going to utilize that scanner class. And what you're going to do is you're going to take in five doubles, you're going to take in five doubles. And then you're going to find the average of those doubles.

So I believe you've already done this. But now you're going to do it using the scanner classic involve two doubles. And third of all, you're going to do it with your new knowledge of operators. And it'll just make a little bit more sense. After you're after you average them. Go ahead and print.

The average of the doubles is and then you know, there was all you got, that will just look a little bit better. So yeah, so that essentially homework for this lesson, I wish you luck with it. It isn't all too difficult. We didn't actually ever go over how we can get doubles from a scanner. But it actually is the same thing is pretty much the same as getting an integer or a string or long or whatever we did. It's essentially the same exact thing for all of it.

So yeah, so I wish you luck. If you really get stuck going to look it up on Google, there are a ton of different resources that you can find for how you can get things From a scanner, engineer speaking, we're at a point now where there's just so much documentation on all of this and tutorials, that finding some problems on Stack Overflow or something isn't really too difficult. And actually, in the next lesson, we're actually going to take the lesson off from coding to research how we can actually find problems, solutions to problems using Stack Overflow and you know, Google in general. So yes, there's going to be next time. Anyway, for now, your homework is to get five doubles, then print the average say that the average of the doubles is and then the average that you got. So yeah, so I wish you luck with the homework 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.