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

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...

ClaimCenter

Claim Center is a web-based enterprise software application designed to manage the process of reporting, verifying, and making payments on claims against a policy. It manages the claims process from first notice of loss through execution of financial transactions, including the payment and setting of reserves. CC Functionality involves Group-based ownership of claims and claim sub objects Claim Financials Address book integration Workspace to manage claims process Distributed collaboration Activity coordination Worker and claim management

JRE vs. JDK

  The JRE and the JDK are two terms you hear people mention very frequently in the Java world, and what they are the two parts that we need in order to run and create Java applications. The JRE is the Java Runtime Environment. JDK is the Java Development Kit. And so, the JRE is what you require in order to run Java applications, and end users normally install the JRE. They're the ones who are going to run our apps. The JDK provides the tools that we need to create Java apps. So, normally, developers are the ones who install the JDK on their machines. And in order to develop apps, you need to run them, so the JDK installation includes a JRE. So how does all this work together? If we sit down, and we type up a Java source file with our program code inside of it, and we want to go ahead and run that in some host environment, how do we do it? A host environment might be Windows, Linux. It could be Mac. It could even be a browser. It might be a phone running Android. How do we get from ...