Copyright

Division in Java: Code & Examples

Instructor: David Gloag

David has over 40 years of industry experience in software development and information technology and a bachelor of computer science

In this lesson, we'll take a look at the division operation in Java, how it differs from mathematical division, and some examples. At the end, you should have a good understanding of this important operator.

The Role of Calculations

Computers are logic machines. The inherent ones and zeros we often hear about lend themselves very well to the idea of true and false. We use computers to perform various calculations. We determine the balances on our bank accounts, we calculate a number of operational statistics, and we even figure out things like ages and membership days for social media. Some of these calculations can be very complex. So, it will come as no surprise that languages like Java provide a robust set of operators to help programmers keep up with those calculations. One of the operations supported by Java is division.

What is Division in Java?

Division in Java is a mathematical operation performed between two values, called operands, using the division operator (/). From a notation perspective, it would look something like:

  • operand1 / operand2

This operation generates a numeric result. This result is the largest value that when multiplied by operand2 will result in a value less than or equal to operand1. The remainder is either zero, or a positive value less than operand2. In equation form, it would look something like:

  • operand1 = operand2 x result + remainder

You should recognize that this is similar to how it behaves in mathematics.

Java Division vs Mathematical Division

The similarity goes beyond the equation we have developed. In addition, you can use positive, negative, and fractional values for both operands. You can use zero for operand1, but not for operand2. You'll get a syntax error if you divide by a zero constant, and an exception if operand2 is zero during execution. However, Java division and mathematical division differ in one significant way. Each operand has an associated type in Java, and that type can affect the result produced by the operation. Sometimes, the effect is significant.

Examples of Division Use in Java

Let's look at some actual code examples. The explanations for each statement will follow.

int result = 10 / 5;
// statement 1
int result = 11 / 5;
// statement 2
double result = 10.0 / 5.0;
// statement 3
double result = 11.0 / 5.0;
// statement 4
double result = 11 / 5;
// statement 5
double result = 11 / 5.0;
// statement 6
int result = 11 / 5.0;
// statement 7

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support