---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 8 Edition"
java_version: 8
chapter: "Chapter 4. Types, Values, and Variables"
section: "4.2.2"
heading: "4.2.2. Integer Operations"
parent: "4.2. Primitive Types and Values"
source: https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2.2
id: "4.2.2"
url: https://jvm.obfuscated.software/jls/se8/4.2.2.html
raw: https://jvm.obfuscated.software/jls/se8/4.2.2.md
---

# 4.2.2. Integer Operations

The Java programming language provides a number of operators that act on integral values:

- The comparison operators, which result in a value of type `boolean`:

  - The numerical comparison operators `<`, `<=`, `>`, and `>=` ([§15.20.1](15.20.1.md))
  - The numerical equality operators `==` and `!=` ([§15.21.1](15.21.1.md))

- The numerical operators, which result in a value of type `int` or `long`:

  - The unary plus and minus operators `+` and `-` ([§15.15.3](15.15.3.md), [§15.15.4](15.15.4.md))
  - The multiplicative operators `*`, `/`, and `%` ([§15.17](15.17.md))
  - The additive operators `+` and `-` ([§15.18](15.18.md))
  - The increment operator `++`, both prefix ([§15.15.1](15.15.1.md)) and postfix ([§15.14.2](15.14.2.md))
  - The decrement operator `--`, both prefix ([§15.15.2](15.15.2.md)) and postfix ([§15.14.3](15.14.3.md))
  - The signed and unsigned shift operators `<<`, `>>`, and `>>>` ([§15.19](15.19.md))
  - The bitwise complement operator `~` ([§15.15.5](15.15.5.md))
  - The integer bitwise operators `&`, `^`, and `|` ([§15.22.1](15.22.1.md))

- The conditional operator `? :` ([§15.25](15.25.md))

- The cast operator ([§15.16](15.16.md)), which can convert from an integral value to a value of any specified numeric type

- The string concatenation operator `+` ([§15.18.1](15.18.1.md)), which, when given a `String` operand and an integral operand, will convert the integral operand to a `String` representing its value in decimal form, and then produce a newly created `String` that is the concatenation of the two strings

Other useful constructors, methods, and constants are predefined in the classes `Byte`, `Short`, `Integer`, `Long`, and `Character`.

If an integer operator other than a shift operator has at least one operand of type `long`, then the operation is carried out using 64-bit precision, and the result of the numerical operator is of type `long`. If the other operand is not `long`, it is first widened ([§5.1.5](5.1.5.md)) to type `long` by numeric promotion ([§5.6](5.6.md)).

Otherwise, the operation is carried out using 32-bit precision, and the result of the numerical operator is of type `int`. If either operand is not an `int`, it is first widened to type `int` by numeric promotion.

Any value of any integral type may be cast to or from any numeric type. There are no casts between integral types and the type `boolean`.

See [§4.2.5](4.2.5.md) for an idiom to convert integer expressions to `boolean`.

The integer operators do not indicate overflow or underflow in any way.

An integer operator can throw an exception ([§11 (*Exceptions*)](11.md)) for the following reasons:

- Any integer operator can throw a `NullPointerException` if unboxing conversion ([§5.1.8](5.1.8.md)) of a null reference is required.
- The integer divide operator `/` ([§15.17.2](15.17.2.md)) and the integer remainder operator `%` ([§15.17.3](15.17.3.md)) can throw an `ArithmeticException` if the right-hand operand is zero.
- The increment and decrement operators `++` ([§15.14.2](15.14.2.md), [§15.15.1](15.15.1.md)) and `--` ([§15.14.3](15.14.3.md), [§15.15.2](15.15.2.md)) can throw an `OutOfMemoryError` if boxing conversion ([§5.1.7](5.1.7.md)) is required and there is not sufficient memory available to perform the conversion.

**Example 4.2.2-1. Integer Operations**

```
class Test {
    public static void main(String[] args) {
        int i = 1000000;
        System.out.println(i * i);
        long l = i;
        System.out.println(l * l);
        System.out.println(20296 / (l - i));
    }
}
```

This program produces the output:

```
-727379968
1000000000000
```

and then encounters an `ArithmeticException` in the division by `l - i`, because `l - i` is zero. The first multiplication is performed in 32-bit precision, whereas the second multiplication is a `long` multiplication. The value `-727379968` is the decimal value of the low 32 bits of the mathematical result, `1000000000000`, which is a value too large for type `int`.
