Ecco un frammento di codicePerché in JVM Integer è memorizzato come byte e breve?
public class Classifier {
public static void main(String[] args)
{
Integer x = -127;//this uses bipush
Integer y = 127;//this use bipush
Integer z= -129;//this use sipush
Integer p=32767;//maximum range of short still sipush
Integer a = 128; // use sipush
Integer b = 129786;// invokes virtual method to get Integer class
}
}
Ecco byte di codice parziale di questo
stack=1, locals=7, args_size=1
0: bipush -127
2: invokestatic #16 // Method java/lang/Integer.valueO
f:(I)Ljava/lang/Integer;
5: astore_1
6: bipush 127
8: invokestatic #16 // Method java/lang/Integer.valueO
f:(I)Ljava/lang/Integer;
11: astore_2
12: sipush -129
15: invokestatic #16 // Method java/lang/Integer.valueO
f:(I)Ljava/lang/Integer;
18: astore_3
19: sipush 32767
22: invokestatic #16 // Method java/lang/Integer.valueO
f:(I)Ljava/lang/Integer;
25: astore 4
27: sipush 128
30: invokestatic #16 // Method java/lang/Integer.valueO
f:(I)Ljava/lang/Integer;
33: astore 5
35: ldc #22 // int 129786
37: invokestatic #16 // Method java/lang/Integer.valueO
f:(I)Ljava/lang/Integer;
40: astore 6
42: return
quanto vedo per gamma integer tra -128 to 127
utilizza bipush
che spingono un byte nello stack come numero intero valore. in un intervallo -32768 to 32767
utilizza short
come classe wrapper come sipush
. Per il prossimo utilizza Integer. Cosa JVM usa byte e short per memorizzare il valore Integer?
Non è questo un duplicato di https://stackoverflow.com/questions/ 20897020/why-integer-class-caching-values-in-the-range-128-to-127? –
@dystroy la mia unica domanda rimane ancora senza risposta perché byte e short per int? –
Se hai due domande separate, è davvero meglio fare due domande separate. Molte persone potrebbero conoscere la risposta all'una ma non all'altra; dovrebbero dare risposte o no? E come sceglierai quale risposta accettare? Si prega di non combinare due domande in una pubblicazione. –