Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a Java int always 32 bits?

Tags:

java

People also ask

Is Java int 32 or 64-bit?

int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1.

Is integer 64-bit in Java?

In Java, the long data type stores integer on 64 bit while the integer data type stores integer on 32bit.

Is int a 32-bit integer?

The size of a signed int or unsigned int item is the standard size of an integer on a particular machine. For example, in 16-bit operating systems, the int type is usually 16 bits, or 2 bytes. In 32-bit operating systems, the int type is usually 32 bits, or 4 bytes.


Yes, it's defined in The Java Language Specification.

From Section 4.2: Primitive Types and Values:

The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing UTF-16 code units (§3.1).

And additionally from Section 4.2.1: Integral Types and Values:

The values of the integral types are integers in the following ranges:

  • For byte, from -128 to 127, inclusive
  • For short, from -32768 to 32767, inclusive
  • For int, from -2147483648 to 2147483647, inclusive
  • For long, from -9223372036854775808 to 9223372036854775807, inclusive
  • For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535

ints are 32 bits. Should you need more, longs are 64 bits.


Java 8 has added some support for unsigned integers.The primitive int is still signed, however some methods will interpret them as unsigned.

The following methods were added to the Integer class in Java 8:

  • compareUnsigned(int x, int y)
  • divideUnsigned(int dividend, int divisor)
  • parseUnsignedInt(String s)
  • parseUnsignedInt(String s, int radix)
  • remainderUnsigned(int dividend, int divisor)
  • toUnsignedLong(int x)
  • toUnsignedString(int i)
  • toUnsignedString(int i, int radix)

Here is an example usage:

public static void main(String[] args) {
    int uint = Integer.parseUnsignedInt("4294967295");
    System.out.println(uint); // -1
    System.out.println(Integer.toUnsignedString(uint)); // 4294967295
}

As supplementary, if 64 bits long doesn't meet your requirement, try java.math.BigInteger.

It's suitable for the situations where the number is beyond the range of 64 bit long.

public static void main(String args[]){
    
    String max_long = "9223372036854775807";
    String min_long = "-9223372036854775808";
    
    BigInteger b1 = new BigInteger(max_long);
    BigInteger b2 = new BigInteger(min_long);
    
    BigInteger sum = b1.add(b1);
    BigInteger difference = b2.subtract(b1);
    BigInteger product = b1.multiply(b2);
    BigInteger quotient = b1.divide(b1);
    
    System.out.println("The sum is: " + sum);
    System.out.println("The difference is: " + difference);
    System.out.println("The product is: " + product);
    System.out.println("The quotient is: " + quotient);
    
}

The output is:

The sum is: 18446744073709551614

The difference is: -18446744073709551615

The product is: -85070591730234615856620279821087277056

The quotient is: 1