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

  1. 1
    When you divide two integers in Java, the fractional part (the remainder) is thrown away. 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.[1] 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.[2]
    • Advertisement
    Method 2
    Method 2 of 2:

    Floating point division

    1. 1
      If either operand in the equation is of the float or double type, you'll need to use float division. 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.[3] 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).[4]

    About This Article

    Nicole Levine, MFA
    Written by:
    wikiHow Technology Writer
    This article was co-authored by wikiHow staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for wikiHow. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions. This article has been viewed 47,213 times.
    How helpful is this?
    Co-authors: 5
    Updated: March 5, 2023
    Views: 47,213
    Categories: Java
    Advertisement