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]
  2. 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
    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
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
Name
Please provide your name and last initial
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 Home Set JAVA_HOME for JDK & JRE: A Step-by-Step Guide
Compile and Run Java Program by Notepad Compile and Run Java Programs Using Notepad++
Check Null in JavaCheck Null in Java
Check Java Version on a Mac Quickly and Easily Check Java Version on a Mac
Add JARs to Project Build Paths in Eclipse (Java)Add JARs to Project Build Paths in Eclipse (Java)
Create an Executable File from EclipseCreate an Executable File from Eclipse
Calculate Percentage in JavaCalculate Percentage in Java
Update JavaUpdate Java
Program in JavaProgram in Java
Call a Method in JavaCall a Method in Java
Print Double Quotes in JavaSimple Steps to Type a Bunny with Your Keyboard
Create JAR File Create a JAR File With Eclipse
Advertisement

About This Article

Luigi Oppido
Reviewed by:
Computer & Tech Specialist
This article was reviewed 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 141,522 times.
How helpful is this?
Co-authors: 5
Updated: February 7, 2024
Views: 141,522
Categories: Java
Thanks to all authors for creating a page that has been read 141,522 times.

Is this article up to date?

Advertisement