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]

    Community Q&A

    Search
    Add New Question
    • Question
      If 'a' variable is divisible by 2 then print 'a' is even otherwise print odd.
      Jessie
      Community Answer
      Complexified: if (a%2 == 0) { System.out.println("even"); } else { System.out.println("odd"); Ternary Operator: System.out.println((a%2 == 0) ? "even" : "odd"); Explanation: "%" is "Modulus", which returns the remainder after dividing the two numbers. Only even numbers can be divided by two without remainder (0). Ternary: You can put ternary operators (([condition]) ? [statement if true] : [statement if false]) in a println() function but you can't put println() functions in ternary operators
    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
        Name
        Please provide your name and last initial
        Thanks for submitting a tip for review!
        Advertisement

        About This Article

        Thanks to all authors for creating a page that has been read 138,760 times.

        Is this article up to date?

        Advertisement