Download Article Download Article

There are two types of division in Java—integer division and floating-point division. Both types use the forward slash (/) symbol as the operator, following the format dividend / divisor.[1] Read on to learn how to divide two integers (non-decimal whole numbers) to receive an integer quotient, and how to use floating point division to get a decimal result.

Method 1
Method 1 of 2:

Integer Division

Download Article
  1. For example, if you were to divide 7 by 3 on paper, you'd get 2 with a remainder of 1. But when you divide two integers in Java, the remainder will be removed and the answer will just be 2.[2] To use integer division, you'd use this syntax:
    int a = 7;
    int b = 3;
    int result = a / b;
    // result will be 2
    
    • Dividing two integers always results in an integer. If you want to get a decimal result from dividing two integers, use floating point division.
    • If you try to divide an integer by 0 using integer division, you'll get an ArithmeticException error at runtime, even if the program compiles fine.[3]
  2. Advertisement
Method 2
Method 2 of 2:

Floating point division

Download Article
  1. You can also use float division if you are dividing two numbers and want a decimal result. To use this division type, set the dividend and divisor to a float.[4] With our example of 7 divided by 3, our code would look like this:
    float a = 7.0f;
    float b = 3.0f;
    int result = a / b;
    // result will be 2.33
    
    • When dividing by zero with floating point division, the result will be NaN (Not a Number).[5]

Expert Q&A

Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement

Video

Tips

  • When dividing mixed integer and floating point numbers, the floating point values (float or double) are automatically converted to the double type when dividing.[6]
Submit a Tip
All tip submissions are carefully reviewed before being published
Thanks for submitting a tip for review!
Advertisement

You Might Also Like

Compile & Run Java Program Using Command PromptA Beginner’s Guide to Compiling & Running Java Programs
Check Your Java Version in the Windows Command LineUse Easy Windows CMD Commands to Check Your Java Version
Set Java HomeHow to Set JAVA_HOME for JDK & JRE: A Step-by-Step Guide
Compile and Run Java Program by NotepadHow to Compile and Run Java Programs Using Notepad++
Check Java Version on a MacHow to Quickly and Easily Check Java Version on a Mac
Check Null in JavaCheck Null in Java
Add JARs to Project Build Paths in Eclipse (Java)Add JARs to Project Build Paths in Eclipse (Java)
Print Double Quotes in JavaSimple Steps to Type a Bunny with Your Keyboard
Create an Executable File from EclipseCreate an Executable File from Eclipse
Calculate Percentage in JavaCalculate Percentage in Java
Program in JavaProgram in Java
Update JavaUpdate Java
Create JAR FileHow to Create a JAR File With Eclipse
Close a Window in JavaClose a Window in Java
Advertisement

About This Article

Luigi Oppido
Written by:
Computer & Tech Specialist
This article was written by Luigi Oppido and by wikiHow staff writer, Nicole Levine, MFA. Luigi Oppido is the Owner and Operator of Pleasure Point Computers in Santa Cruz, California. Luigi has over 25 years of experience in general computer repair, data recovery, virus removal, and upgrades. He is also the host of the Computer Man Show! broadcasted on KSQD covering central California for over two years. This article has been viewed 111,174 times.
How helpful is this?
Co-authors: 5
Updated: February 7, 2024
Views: 111,174
Categories: Java
Thanks to all authors for creating a page that has been read 111,174 times.

Is this article up to date?

Advertisement