Is a Java int always 32 bits?

Java

Java Problem Overview


Will Java's int always and everywhere be a 32 bit signed integer?

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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

Solution 3 - Java

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
}

Solution 4 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionppQuentinView Question on Stackoverflow
Solution 1 - JavacoobirdView Answer on Stackoverflow
Solution 2 - JavadrRoflolView Answer on Stackoverflow
Solution 3 - JavaroblovelockView Answer on Stackoverflow
Solution 4 - JavaEugeneView Answer on Stackoverflow