Skip to main content

Arithmetic Operators

 

Something we commonly must do in our programs is perform arithmetic operations. Java provides three basic categories of operators for performing math operations. They're the basic operators, like add, subtract, multiply, and divide, what are called prefix and postfix operators, and then what are called compound assignment operators. The basic math operators are just what you would expect, things like add, subtract, multiply, and divide. The operators we use for these are, again, things that are very familiar. If I'm adding, I use a plus sign. And when I do things like add, it doesn't really matter whether I'm using a floating point or an integer. So, if I say 1 + 2, the answer is going to be 3. For subtraction, we use the minus sign. Again, it doesn't really matter whether we're using floating point or integers. If I subtract 4 from 5, I get 1. If we multiply, we use an asterisk, 4 * 2 is 8. And all that stuff is straightforward. As we get to the divide operations, though, there are some differences between floating point and integer. If I take 13.0 and divide it by 5.0, that's a floating-point operation. Floating points have fractions, so I will get 2.6. But now if I take the integer 13 divided by 5, that's an integer operation. Integers don't have fractions, so I'll get just 2. It doesn't matter what the fractional portion would be. It's just dropped. There's no rounding or anything. It's just the whole number of times that you can do the division. So, 5 goes into 13 two whole times. There's another operator called the modulus operator, also known as the remainder operator, and that gives you the remainder of the divide. So, let's look at the integer first on this one. If I say 13 % 5, that's the percent symbol, my answer will be 3. Five goes into 13 two whole times, leaving 3, 5 * 2 is 10, subtract 10 from 13, you get 3. And you can do a modulus on floating points as well. So, if I do 13.0 % 5.0, I'll have a remainder of 3.0. As we look at the prefix and postfix operators, they allow us to operate directly on a value. The ++ symbol is the increment. It increments a value by 1. The ‑‑ symbol decrements a value by 1. So, if you look here, if I have a variable, myVal, and I set it to 5, if I say print out ++myVal, that's the increment operator. Because it's before the actual variable, the ++ is before the variable, that's a prefix, which means that the operation is applied before we get the value back. So, the result of this print statement is 6, the 5 was incremented, so we print out it was 6. If I print myVal again, it's still 6. But now if I move the operator after the variable, it's now a postfix operator. And what that means is that I get the value back, and then the operation is performed. So, if I look at similar code here where I have my variable, myVal, I set it to 5, but this time when I print it out, I say myVal++, so I'm doing a postfix increment. The value printed will be 5 because that 5 was returned. But myVal is now incremented. If I print myVal now, I will get 6. The last category of math operators is what we call compound assignment operators. And what these do is they combine an operation and the assignment. So, basically, it looks at the right side of the operator, takes whatever that is, and applies the operation to the left side, and then stores the value into the left side. So, let's see what that looks like. If I go ahead and have myVal, and I set it equal to 50, if I say myVal = 5, so the right hand side's value is 5, the left hand side value is currently 50, it applies the operation, which is the minus sign, so it subtracts 5 from 50, and then stores that value back into myVal. So, if I print out myVal, myVal has 45. And they're available for all five basic math operations. In my experience, I use the = and the += far and away the most, but you do find occasions to use the other ones as well. So looking at another example, if I have an int result = 100, and I have 2 more variables, val1 = 5 and val2 = 10, if I now say result /= val1 * val2, remember that it's going to take the complete result of the right side. So, if I look a val1 * val2, the result of 5 * 10 is 50. It then takes the existing value in result, which in this case is 100, and then it then takes that operator, the divide, and performs that operation between them. So, it's going to divide 100 by 50 and then store that back into our variable called result. So, we print out the value of 2.

Comments

Popular posts from this blog

Difference Between Programming and Coding

What exactly is the difference between programming and coding?  The other day, one of my friends who is not from a computer science background, asked me this question. Even after learning many different programming languages and doing several projects, I could not answer him correctly. I said both are the same. But why do we use two different terms, if both are the same? That led me to some research, and I thought I’d share what I found.   It’s not that complicated.  And their definitions allow for a lot of overlap. We often recognize the terms coding and programming as synonymous because both are often used interchangeably.   what is the difference between programming and coding? Coding is the act of expressing programmatic ideas in computer language. Programming is crafting ideas that can be executed repeatedly by a machine, not necessarily a computing device. While both the terms are synonymous with each other and are often used interchangeably, t...

Types of Recording

Types of Recording: 4 types of recording modes are available Ø   Basic Ø   Desktop Ø   Web Ø   Citrix 2 ways of Recording Ø   Automatic Ø   Manual Basic – generates a full selector for each activity and no container, the resulted automation is slower than one that uses containers and is suitable for single activities. Desktop – suitable for all types of desktop apps and multiple actions; it is faster than the Basic recorder, and generates a container (with the selector of the top-level window) in which activities are enclosed, and partial selectors for each activity. Web – designed for recording in web apps and browsers (supported: Internet Explorer, Google Chrome), generates containers and uses the Simulate Type/Click input method by default. Citrix – used to record virtualized environments (VNC, virtual machines, Citrix, etc.) or SAP, permits only image, text and keyboard automation, and requires explici...

Variables, Data Types and Math Operations

  In order to do anything interesting in a program, we must have the ability to store and manipulate values. What allows us to do that are what we call variables. Now a variable, simply put, is just named data storage. Now Java is a strongly ‑ typed language. Now what that means is that when we declare a variable like, in this case, we have a variable named data Value, we have to specify the type of that variable, in this case, it's what we call an int, something that can store integers. So, what that means is that the data Value variable can only store things that are compatible with the type int. As we go through the course, we'll talk about this idea of one type being compatible with another. Now when we declare a variable, we can, of course, then assign a value to it, so our variable data Value now holds the value 100. Now when we use variables, we can do it the way we've done here where we declare it, then assign it, or as a matter of convenience, we can declare them a...