2009-06-23 11 views

risposta

101

Sì, è definito in The Java Language Specification.

Da Section 4.2: Primitive Types and Values:

I tipi integrali sono byte, short, int e long, i cui valori sono 8 bit, 16 bit, 32 bit e 64 bit con segno two's- complemento numeri interi, rispettivamente, e char, i cui valori sono numeri interi senza segno a 16 bit che rappresentano unità di codice UTF-16 (§3.1).

E inoltre da Section 4.2.1: Integral Types and Values:

I valori dei tipi integrali sono interi nei seguenti intervalli:

  • Per byte, da -128 a 127, compreso
  • In breve, da -32768 a 32767, compreso
  • Per int, da -2147483648 a 2147483647, compreso
  • Per molto tempo, da -9223372036854775808 a 9223372036854775807, comprensivo
  • Per char, da '\ u0000' a '\ uffff' inclusiva, cioè, da 0 a 65535
+1

Informazioni eccellenti :-) – joe

7

int s sono 32 bit. Se hai bisogno di più, gli long s sono a 64 bit.

0

Java 8 ha aggiunto il supporto per gli interi non firmati. Il primitivo int è ancora firmato, tuttavia alcuni metodi li interpreteranno come non firmati.

I seguenti metodi sono stati aggiunti al Integer class in Java 8:

  • compareUnsigned (int x, int y)
  • divideUnsigned (int dividendo, int divisore)
  • parseUnsignedInt (String s)
  • parseUnsignedInt (String s, int radix)
  • remainderUnsigned (int dividendo, int divisore)
  • toUnsignedLo ng (int x)
  • toUnsignedString (int i)
  • toUnsignedString (int i, int radix)

Ecco un esempio di utilizzo:

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

Come complementare, se lungo 64 bit non soddisfa i tuoi requisiti, prova java.math.BigInteger.

È adatto per la situazione in cui il numero è oltre l'intervallo di 64 bit.

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); 

} 

Il risultato è:

La somma è: 18446744073709551614

La differenza è: -18446744073709551615

Il prodotto è: -85070591730234615856620279821087277056

Il quoziente è: 1

Problemi correlati