COURSES
MBAData Science & AnalyticsDoctorate Software & Tech AI | ML MarketingManagement
Professional Certificate Programme in HR Management and AnalyticsPost Graduate Certificate in Product ManagementExecutive Post Graduate Program in Healthcare ManagementExecutive PG Programme in Human Resource ManagementMBA in International Finance (integrated with ACCA, UK)Global Master Certificate in Integrated Supply Chain ManagementAdvanced General Management ProgramManagement EssentialsLeadership and Management in New Age BusinessProduct Management Online Certificate ProgramStrategic Human Resources Leadership Cornell Certificate ProgramHuman Resources Management Certificate Program for Indian ExecutivesGlobal Professional Certificate in Effective Leadership and ManagementCSM® Certification TrainingCSPO® Certification TrainingLeading SAFe® 5.1 Training (SAFe® Agilist Certification)SAFe® 5.1 POPM CertificationSAFe® 5.1 Scrum Master Certification (SSM)Implementing SAFe® 5.1 with SPC CertificationSAFe® 5 Release Train Engineer (RTE) CertificationPMP® Certification TrainingPRINCE2® Foundation and Practitioner Certification
Law
Job Linked
Bootcamps
Study Abroad
Master of Business Administration (90 ECTS)Master of Business Administration (60 ECTS)Master in Computer Science (120 ECTS)Master in International Management (120 ECTS)Bachelor of Business Administration (180 ECTS)B.Sc. Computer Science (180 ECTS)MS in Data AnalyticsMS in Project ManagementMS in Information TechnologyMasters Degree in Data Analytics and VisualizationMasters Degree in Artificial IntelligenceMBS in Entrepreneurship and MarketingMSc in Data AnalyticsMS in Data AnalyticsMaster of Science in AccountancyMS in Computer ScienceMaster of Science in Business AnalyticsMaster of Business Administration MS in Data ScienceMS in Information TechnologyMaster of Business AdministrationMS in Applied Data ScienceMaster of Business AdministrationMS in Data AnalyticsM.Sc. Data Science (60 ECTS)Master of Business AdministrationMS in Information Technology and Administrative Management MS in Computer Science Master of Business Administration MBA General Management-90 ECTSMSc International Business ManagementMS Data Science MBA Business Technologies MBA Leading Business Transformation Master of Business Administration MSc Business Intelligence and Data ScienceMS Data Analytics MS in Management Information SystemsMSc International Business and ManagementMS Engineering ManagementMS in Machine Learning EngineeringMS in Engineering ManagementMSc Data EngineeringMSc Artificial Intelligence EngineeringMPS in InformaticsMPS in Applied Machine IntelligenceMS in Project ManagementMPS in AnalyticsMS in Project ManagementMS in Organizational LeadershipMPS in Analytics - NEU CanadaMBA with specializationMPS in Informatics - NEU Canada Master in Business AdministrationMS in Digital Marketing and MediaMS in Project ManagementMaster in Logistics and Supply Chain ManagementMSc Sustainable Tourism and Event ManagementMSc in Circular Economy and Sustainable InnovationMSc in Impact Finance and Fintech ManagementMS Computer ScienceMS in Applied StatisticsMS in Computer Information SystemsMBA in Technology, Innovation and EntrepreneurshipMSc Data Science with Work PlacementMSc Global Business Management with Work Placement MBA with Work PlacementMS in Robotics and Autonomous SystemsMS in Civil EngineeringMS in Internet of ThingsMSc International Logistics and Supply Chain ManagementMBA- Business InformaticsMSc International ManagementMBA in Strategic Data Driven ManagementMaster of Business AdministrationMSc Digital MarketingMBA Business and MarketingMaster of Business AdministrationMSc Digital MarketingMSc in Sustainable Luxury and Creative IndustriesMSc in Sustainable Global Supply Chain ManagementMSc in International Corporate FinanceMSc Digital Business Analytics MSc in International HospitalityMSc Luxury and Innovation ManagementMaster of Business Administration-International Business ManagementMS in Computer EngineeringMS in Industrial and Systems EngineeringMSc International Business ManagementMaster in ManagementMSc MarketingMSc Business ManagementMSc Global Supply Chain ManagementMS in Information Systems and Technology with Business Intelligence and Analytics ConcentrationMSc Corporate FinanceMSc Data Analytics for BusinessMaster of Business AdministrationMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSBachelors in International ManagementMS Computer Science with Artificial Intelligence and Machine Learning ConcentrationMaster of Business Administration
For College Students

Java Arithmetic Operator Part I - An overview

$$/$$

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.

$$/$$

Video Transcript

 

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.

 

Video Recap

 

  • 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:

  1. Arithmetic operators(+,-,*,/) are used for addition, subtraction, multiplication and division respectively.

  2. An operation performed on variables of int data type will always return an int value only and hence can lead to information loss.

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