Popular Google Pages:

◕ Bengali Story writing competition. More..

This article is regarding **Errors in Integer Arithmetic in Java.**

Last updated on: 20th March 2017.

◕ Errors in Integer Arithmetic in Java.

When we perform any arithmetic calculation with
integer type in Java then sometimes we don't get the proper result.

We get these errors for the following reasons.

■ If we divide an integer value by zero. Then we get an error.

■ If we use the % operator with an expression that has a Zero value at the right hand side.

Example: **restNum = totalNum % 0;**

■ If an integer expression results a value that is outside the range of the **int type**, then the result is
truncated to the number of bits upto the **int type** range. Hence we get an incorrect result. But Java does not indicate this as an error though the result is incorrect.
Therefore we should careful for this.

■ Again we can get an interesting result. Let have an example statement first.

**totalNum = ( 3000000 * 4000000 ) / 2000000;**

In this case we will get a wrong answer.

Because ( 3000000 * 4000000 ) this part produces an answer that exceeds the range of the ** int type.**

As a result the answer is already truncated before the division takes place.

Obviously after the division we will get the wrong answer again.

And the Java will not show any error for this wrong answer.

Related Java articles:

■ What is Java? Where we can apply Java?

■ Why Java is a Machine Independent Program? What is Machine Independent Program?

■ Root Directory of Java

■ How to Compile and run a Java Program?

■ What is .class file in Java?

■ What is variable in a program?

■ What is identifier in Java?

■ Integer data types in Java?

■ What is Primitive Types in Java?

■ How to declare constant variables in Java?

■ Why should we declare the initial value of a variable in Java?

■ Use of Underscores to write an integer in Java

Top of the page