Join David Gassner for an in-depth discussion in this video Using mathematical operators and the Math class, part of Java 8 Essential Training.
- Java supports the standard mathematical operators that you'll expect to see in all programming languages. But it also supports more complex operations using a special class called Math. I'm working in the Math project now and I've started by declaring two integer values. I'll add them together using code that's pretty intuitive. I'll declare another integer and I'll name it result1 and I'll get its value by adding intValue1 and intValue2 using the + operator.
Then I'll output to the console and I'll use a label of "Addition: " and I'll append my result. I'll run that code and I'd get back an expected result. Now, I'm going to copy and paste these lines of code four times and I'm going to use four other operators. Then I'll change the operators that I'm using. I'll use the subtraction operator, the multiplication operator, the *, division which is a / and remainder which is a % character.
Then I'll change the labels to match the operators and I'll run the code. For the first three operations and for the last one, I get back the expected result. Addition, subtraction and multiplication are exactly what you expect to see and the remainder again is what you expect. But division isn't quite right. Here's why. Whenever you do a mathematical operation, the returned result will be a data type that matches the widest data type of any of the parts.
So if I start with two ints, I'll get back an int. If I start with two doubles, I'll get back a double. But if I start with a double and an int, I'll still get back a double. To do a fractional operation where I'm expecting a fractional value back, I have to start with double or floating values. So I'm going to add casting syntax here. I'll say that this is a double value instead of an int value. I get an error indicator and I'll press alt or opt and the enter or return key and in my list I see Cast to 'int', that's not what I wanna do, I started off with an integer, or I see Change variable 'result' forward to 'double', that's what I want.
I'm saying "I know I'm going to be getting back a fractional value "so I'll set the data type appropriately "from the beginning." I'll run the result and now I get back a result that I expect. Typically, I'll do the same thing when I'm using remainders as well. I still get back 14 but now it's 14.0. So those are the operators and setting your data types correctly. As I mentioned, you can also do complex operations using the Math class.
The Math class is a member of the Java.lang package so you don't need any special imports or other notation to use it, you simply start off with the word Math, with an uppercase M because it's a class identifier, and then the method that you wanna use. Let's say for example that I want to round a value. I'm going to declare another value, this one will be a double and I'll give it a negative number of 3.99999. Now, I want to round that value.
There's a method in the Math class named round and it returns a long value so I'll declare a long value that I'll name rounded and I'll call Math and when I pressed period I see a list of all the different methods that are available. I'll start typing round and I see that there are two versions, one to round a double and one to round a float. I'll use the double version and I'll pass in my double value. Then I'll output the result to the console, I'll start with a label of "Rounded: " and then I'll output the variable rounded.
When I run the code, I see that the rounded value is -4. The round method has rounded to the nearest integer value which in this case is 4. But let's also say that I wanna take that value and make it absolute, that is, always a positive value. This time I'll declare a double value and I'll name it absValue and I'll call Math.abs. Notice that there are a number of different versions of this method for various primitive numeric data types.
I'll choose the one that accepts a double and I'll pass in doubleValue. Then I'll output to the console with a label of "Absolute: " and I'll append the returned value, absValue. I get back a positive 3.99999. The Math class supports many other operations. Check the documentation for everything it's able to do. I'll place my cursor in the name of the class and press shift + F1 and that takes me to the documentation for the class.
If you see a little menu pop up showing different sources of the help, you can choose either one. Then I'll click METHOD and that takes me to a list off all the different methods that are members of this class. Notice that the different methods return different data types, depending on what kind of operation they represent. But they're all static, meaning that you call them directly from the class definition, as I have here with Math.round and Math.abs.
You don't need to create an instance of the Math class to call these methods. The Math class and the built-in mathematical operators let you execute whatever mathematical operations you need to on primitive values.
- Understanding the history and principles of Java
- Installing Java, IntelliJ IDEA, and BlueJ
- Creating a Java project
- Working with variables, values, and expressions
- Working with object data types
- Building, comparing, and parsing strings
- Debugging and exception handling
- Creating loops and reusable code
- Passing arguments by reference or value
- Using simple and complex arrays
- Creating custom classes
- Understanding inheritance and polymorphism
- Managing files with Java libraries
- Documenting code with Javadoc
- Packaging classes in JAR files