image
Arithmetic Operators
Start course
Difficulty
Intermediate
Duration
2h 7m
Students
373
Ratings
4.8/5
Description

This course explores the fundamentals of Java and puts them to use with some real-life examples looking at an average of three program and a mad libs project.

Learning Objectives

  • Learn how to print information out to the user, how to create and use variables, values, and constants, how these things have data types, and the differences and similarities among the data types
  • Learn about arithmetic, relational, and logical operators
  • Understand how to obtain input from the user of our programs

Intended Audience

  • Beginner coders or anyone new to Java
  • Experienced Java programmers who want to maintain their Java knowledge
  • Developers looking to upskill for a project or career change
  • College students and anyone else studying Java

Prerequisites

This is a beginner-level course and can be taken by anyone with an interest in learning about Java.

Transcript

In the previous lectures, we have learned how to work with simple variables, constants, and data types, as well as how to provide basic documentation for our code using comments. In this lecture, we will expand our knowledge and learn how to perform various operations on the variables and literals that we come in contact with. We will start with arithmetic or arithmetic operators which we can use to operate on our data. In our Section2 Project, let's create another Java file called ArithmeticFun. So, source, right click 'New', 'Java Class', ArithmeticFun. Good. Now let's create the skeleton program. There we go. Public static void main. You can make sure that you type it out. I've been using a shortcut but very easily you need to be able to do this and once you get to that point you can use the shortcut and whatever ID you're using. Usually I type psvm for public static void main and hit 'Tab', but typing it out should not be difficult either. public static void main (String[] args) { }. So, just make sure that you know how to do this basically in your dreams and when you're asleep, when you're awake, and when you're driving that you know how to write this out very easily.

If you don't remember the exact syntax then don't use the shortcut. We see some of the arithmetic operators right now and the different arithmetic operators are addition operator which is the +. The - is the subtraction operator. You've got the multiplication operator which is *, which on most keyboards is shift 8. You also have / which acts as the division operator and then the % might be a little bit confusing, that's called the modulus operator and it gives you the remainder after a division problem is performed. But let's do an example and see what we can come up with. So, in ArithmeticFun let's add some code and see what happens. int a = 10, int b = 15. Then int result = a + b. System.out.println ("result is" + result). Also shortcut for that sout and then hit 'Tab' and that's basically like auto-complete in Microsoft Word or something like that. 

It's a similar thing but you should be able to do this without any trouble. But once you're used to doing it then using the shortcuts is not that big of a deal. But prior to that I would highly recommend you type them out for practice. And even though I know them very well, every few projects I create on my own I will actually type out everything without using any of the shortcuts, just to make sure that my muscle memory is good. Nice. So, let's run the application and see if we get the expected output here. So, I'm going to run ArithmeticFun, right-click 'Run 'ArithmeticFun.main()''. And it says the result is 25. So, hopefully no surprise there. We created these two variables right here. We have a and b. a = 10, b = 15 as their values and then we have int result = a + b. So, right here a = 10, b = 15. So, when we add them together using this addition operator we get 25.

So, again just to make sure we're good on terminology here, in general we call + an operator and this specifically is the addition operator and then the elements that it's trying to combine by using addition and return value. These elements the a and the b are actually called operands, O-P-E-R-A-N-D-S, operands. So, this one is a binary operator because it takes two operands. Now = is also an operator right here, this is the assignment operator and what that does is it sets the value of result to whatever is returned by a + b. So, there's actually a lot going on here. So, let's some of this review and some of this is diving a little deeper into the material. First we declare and initialize our two integer variables a and b. Then we declare and initialize the third called result which instead of setting it to the value of a literal constant like we did the other two.

So, 10 and 15 are literal constants, literal values. We place an addition operator in between the two operands to form an addition operation. This operation consists of again the addition operator and both of the two operands on either side a and b. When one of the operands of the + is a string, then the + is no longer the addition operator. It is called the concatenation operator which we saw earlier. So down here, this is actually concatenation even though this is an integer right here the result. Since there's a string as one of the operands that makes this concatenation, because you're not really adding and into during a string, you can't really do that, but you can concatenate. So, there are two operations again involved in the statement. There's the addition operator, but there's also the assignment operator which captures the result. So, in the case where a = 10 and b = 15, a + b yields the value 25. So, result is set to 25 using the assignment operator, =. Now, let's create a few more variables and perform some more operations. So, below result which is really just a sum. We're going to create difference, product a * b. We're going to do quotient. 

Now, be careful here b / a and int remainder = b % a that's the percent sign, and now let's do some printing. So, I'm going to actually go down here and we're going to say "diff" or difference. You can spell it out. Not a big deal doesn't really matter. And we're also going to have ("product is" +product). The ("quotient is" + quotient), ("remainder is" + remainder). All right, let's run it now. ArithmeticFun and then we go down to Run 'ArithmeticFun.main()'. Now, we get 25 for the sum, you get -5 for the difference because we have a - b, 10 - 15. So, that gives us the -5. You have the product is 150, 15 * 10. Quotient is 1 and remainder is 5. So, the quotient is 1 because when you be divide 15/10 since we're dealing with integers, this performs integer division. And what happens there is it, says okay, 110 can fit inside of 15. So, that's one. What's leftover once we do that? Well, that's what the module operator tells us and that's the value 5. Because 10 can only fit in 15 once, but it has leftover of five. So, what's basically happening when we divide 15 / 10, which is a little bit of a strange output that you may not expect because you might be thinking of it should be 1.5. But again, it's integer division because there's two integers involved. Now, what happens to that value even though we can reproduce it using the modulus operator and get what was leftover. What actually happens just on the line where we're grabbing the quotient. When we take the 15 and divided by 10, it truncates the value that would have been in the decimal spot because it's kind of like rounding down but the reality is it just loses the data. So, how many 10s fit into 15?

Only 1. And that's what integer division does. Now, if one of these values was a double, it would return a double value as a result. So, if the a or the b was a double and then to actually hold the double value, we would actually have to use a double quotient as well.

So, again, just to review and to make sure we're on solid ground here, modulus is a little bit foreign to some people. So, let's make sure we know what's going on. The % is what we use for modulus and remember that data we lost during integer division? Well, modulus can help us get it back. It returns the remainder of the division operation. So, 15  % 10 answers the question. If we divide 15 / 10, what is the remainder? That is what's leftover after the dvision. So, 10 goes into 15 once and we have 5 remaining, but our 10 can't fit into that. So, that's what's leftover. That's where the quotients 1, remainder is 5. So, hopefully, that makes sense. I know I went over it multiple times. We want to practice it and make sure that we understand it really thoroughly before moving to deep into the course. Now, let's quickly look at a compound assignment operator. The compound assignment operators are the operators where you have one of the arithmetic operators and they are each followed by an equal sign, the assignment operator. So, they're really just shorthand. 

Now, let's look at one in the code and see what happens here and then we'll see what the output is. So, result += 20. So, we're taking the initial, result = a + b, and we're now adding 20 to it and let's print it out again. System.out.println. I'm going to say, "result is now". All right, so, now let's run this again. Right-click and we now get the value result is 45. So, it started with the 25 initially and then it added 20 to it and started backend result. So, really what this is, is it's shortcut for result = result + 20. So, you take whatever's on the right. It could be a literal, it could be a variable, it could be a method call anything that resolves to a compatible value. In this case, an integer and the += says, take that thing on the right, add it to the thing on the left, and then assign the result to the thing on the left. Every one of the arithmetic operators has a corresponding compound assignment operator. So, they just act as shortcuts syntax. So, result += 20 again is the same as a result = result + 20. But we can also have results or anything result = result times 20 or you can do result times = 20. Does that makes sense?

So, there's no space between these two symbols right here. So, these are compound operators. So, you can use the times equals, divide equals, minus equals, or even modulus equals.

With the regular arithmetic operators, you can use two variables, of variable to literal or to literals. With the compound operators, you must have a variable on the left of the operator. But you can have a literal variable or anything that resolves to a comparable value on the right. Regardless, any operator that takes two operands can be classified as a binary operator. Finally, there is actually an even shorter way to add or subtract one from a variable. We'll use it a lot more later in this course but for now, just know that adding or subtracting one from a variable is a very common task. It's so common that many programming languages have the shortcuts known as increment and decrement operators. 

So, right here, let's in the bottom here, result ++, and I'm going to print it. So, System.out.println result++ and then I'm just going to write an arrow, doesn't really matter and then write the result, maybe I'll take that out because that looks little bit too much like an operation. And now result--. System.out.println result-- and then we're going to concatenate the result again. And let's see what happens when we run it. So, we now get all the values that we expected. The 45 is what we ended up with here. But you'll now notice that we get a 46 because the value was incremented and then a 45 because we decremented it back to 45. So, you can also use this form and when they're on their own line, it doesn't really matter what you do. So, --results. They do the same thing when they're on the same line by themselves. So, there are circumstances where they're different but we'll get to that way later in the course. Now, it's pretty cool.

We've got the results we expect here. I mentioned that when an operator has two operands, like result += 20 or even quotient equals b / a,  or anything like that, the b / a, the division is a binary operator. The compound addition assignment operator here is also a binary operator because it has two operands. But down here you'll notice that this is acting upon result. This operator right here, the increment operator is acting upon result even though there's only one operand. Result's the only operand. So, when that happens we have a unary operator. So, we have these would be examples of binary operators because they take two operands and then down here I'm going to say, these are examples of unary operators because they each take one operand. Pretty cool. 

Now, before we move on, I'd like to issue you a challenge. Write some code in our ArithmeticFun Java file to multiply product, the variable product, by two and store that value back into the product variable and print it out. As a hint, use a compound operator to help you. And also you're not limited to using just the two variables. And don't forget to print it out so you can double check remember that. So, pause the video and attempt this challenge. Come back when you're done or if you need some help. How did that go for you? Were you able to accomplish this challenge? Great job if you were. And even if you weren't, good job for trying, don't get discouraged. You'll get better as you go through more and more Java code. All right, let's do this together and make sure that you know how. product times = times equals.

There we go, multiplied by two, right? So, that's equivalent to product equals, product times two. And now, I want you to print it out. So, now at the end we'll say final value of product is and then product. All right, let's run it and let's see what we get. So, right click, 'Run', 'ArithmeticFun.main' and we get the final value of product is 300. It was 150 up here. We multiplied it by two and assigned the value back on the product variable and that's what we got. So, great work. We were able to perform this little shortcut using the compound multiplication assignment operator. To double the value of product and then use the concatenation operator to help us print out the value of the new product within the message. In the next lecture, we're going to continue our discussion of operators, looking at a different kind of operator, that will later help us to make decisions in our programs. The relational operators. I'll see you there.

 

About the Author
Students
1772
Courses
20
Learning Paths
4

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics