In this segment, you will learn how to make calculations in Java. Java, at times, surprises you with the kind of results it comes up with when you perform calculations. Let’s learn why it does this and obviously, the syntax used for all the calculations.
In Java, +, -, and * are the symbols used for addition, subtraction, and multiplication respectively.
For division, the “/” symbol is used. Let us now take a look at a particular case of division in Java.
So till now you have learned how to write simple print statements in Java, as well as how to declare variables, assign value to them, update them, and also you saw various different data types in Java. So now you can do a lot of stuff in your computer programs. For example, you can perform different calculations, you can add a lot of numbers, you can multiply or divide them. So you can do a lot of arithmetic operations on these numbers. And addition, subtraction and multiplication are quite easy and basic forms of arithmetic operations. You can read more about them in the text. However, there is one case which is a little tricky when it comes to Java programs and that is the case of dividing two numbers. And why is that tricky? Well, it is because the division of two numbers may not always result in a whole number. That is, it might be possible that you take two whole numbers, you divide them and you get a decimal number. So it makes up for many different curious cases in Java which we are going to take a look at right now. So let me take two integers here, say int x equals to three and int y equals to two. Now, suppose I wish to divide these two integers and store them in another variable, z. I say that int z equals to x divided by y. This slash is a symbol for division. So now what happens is that when the Java program encounters these two variables, it treats them strictly as integers. That is, they are three and two. And when you divide any two integers, the result is always going to be an integer, no matter what the result of the actual mathematical operation might be. For example, in real life, if you divide three by two, you would say that the answer is 1.5. However, in this case, when my x and y are both integers, then the result is always going to be an integer, no matter how many digits there are after the decimal. So basically this means that when I do three divided by two, the result of this operation is going to be what? Well, it is going to be one. And why is it one? It is because when I divide three by two, the result, which is 1.5, gets rounded off to its lower base value. That is, 1.5 gets converted. It gets converted into one. So the bottom line of this whole situation is that on dividing any two integers, the result is always going to be an integer and that integer is going to be the lower base value. It does not matter whether on dividing two numbers I get 1.1 or 1.9 or 1.5, whenever I'll divide two integers, all of these three are going to get rounded off into the lower base value, which is one. So here, what do you think will be contained in the variable Z? Well, x is an integer, y is an integer. So the result is definitely going to be an integer. In this case, three divided by two will give me the value one and so on printing the value of Z, I would get that z equals to one. Now, if we are trying to be clever here, maybe we can try to store x divided by y into a double data type. You remember, double is a data type which is used to store all such numbers which contain some decimal value. That is, a number can be like 2.3456. So when I have to store such decimal numbers, I can use the double data type. So maybe, perhaps we can just try and see that when I divide x by y, maybe if I wish to store it into double, let's see what the result will be. So I say that double D is equal to x by Y. So now you would be tempted to believe that this D would consist of three divided by two, which will be 1.5. And since it is double, it will be able to store 1.5. But then this is not true because we just now learned that the division of any two integers in Java is always going to be an integer. So even in this statement, when Java program encounters this operation x divided by y, it does three divided by two. And since both three and two here are integers. So again, the result is going to be an integer, which will be the lower base value of this operation. So what will be the result of three divided by two? It will be one again.
So as soon as the program calculates that x divided by y is again one, it then sees that this is actually a double. So what will be the value stored inside this variable? D. So this integer one will then be converted into a double value and so the value of D will be 1.0. So basically it does not matter whether the result of my division operation, I store it in an int, or if I store the result of my division operation in a double. In both the cases x by y operation is going to treat x and y both as integers and will always only give me an integer. So in both the cases int as well as in double, the result is going to be one. However, when I print Z, the result is going to be one. And if I print D, the result is going to be 1.0. But essentially the value of both of them are going to be the same because the division of two integers is always going to give me an integer value.
Java basics: print statements, variable declaration, data types, arithmetic operations
Division of two numbers in Java may not always result in a whole number
When dividing two integers, result is always an integer and rounded to lower base value
Result of division of two integers in Java will always be an integer, regardless of data type used to store it
Double data type used to store numbers with decimal values
Dividing two integers and storing the result in a double still results in an integer value, which is converted to a double
Result of division of two integers in Java is always an integer value, whether stored in int or double data type.
In the previous video, you learnt that ‘/’ is used for division in Java.
Also, in the case of multiple calculations, the general BODMAS rule is followed. Remember, to prioritise something, you use parentheses.
But what about the cases when you want the result as 0.5? Let us take a look at one of the ways to prevent this information loss.
A few important takeaways till now are:
Dividing two integers will always give back an integer. So dividing 5 by 4 will give you 1 and not 1.25. The portion after the decimal is ignored.
Similarly, dividing two doubles always gives back a double. So dividing 4 stored in a double variable by 2 stored in a double variable will give back a double, 2.0 and not an int, 2.
Now that you know how arithmetic operations work in Java, let us try to solve some problems based on the same.
You now know the following about arithmetic operations in Java:
Arithmetic operators(+,-,*,/) are used for addition, subtraction, multiplication and division respectively.
An operation performed on variables of int data type will always return an int value only and hence can lead to information loss.
If you want to avoid information loss, one way is to use appropriate data types in the first place depending on your purpose.
In the next segment, you will learn about something called the “mod” operator and see all the Java code in action on the IntelliJ IDE.