PDF download Download Article PDF download 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

PDF download 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]
    • Advertisement
    Method 2
    Method 2 of 2:

    Floating point division

    PDF download 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
        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

        About This Article

        Thanks to all authors for creating a page that has been read 117,362 times.

        Is this article up to date?

        Advertisement