Integer.MAX_VALUE e Integer.MIN_VALUE in Java con esempi
Il più delle volte, nella programmazione competitiva, c'è la necessità di assegnare alla variabile, il valore massimo o minimo che il tipo di dato può contenere, ma ricordare un numero così grande e preciso risulta essere un lavoro difficile. Pertanto Java dispone di costanti per rappresentare questi numeri, in modo che questi possano essere assegnati direttamente alla variabile senza digitare effettivamente l'intero numero.
- Intero.MAX_VALUE
Integer.MAX_VALUE è una costante in Classe intera del pacchetto java.lang che specifica che memorizza il massimo valore possibile per qualsiasi variabile intera in Java. Il valore reale di questo è
2^31-1 = 2147483647
Esempio 1:
// Java program to show> // the value of Integer.MAX_VALUE> > class> GFG {> > > // Driver code> > public> static> void> main(String[] arg)> > {> > > // Print the value of Integer.MAX_VALUE> > System.out.println(> 'Integer.MAX_VALUE = '> > + Integer.MAX_VALUE);> > }> }> |
Produzione:
Integer.MAX_VALUE = 2147483647
Qualsiasi variabile intera non può memorizzare alcun valore oltre questo limite. Così facendo, la memoria traboccherà e il valore diventerà negativo.
Esempio 2: Tentativo di inizializzare un valore variabile Integer.MAX_VALUE + 1
// Java program to show what happens when> // a value greater than Integer.MAX_VALUE> // is stored in an int variable> > class> GFG {> > > // Driver code> > public> static> void> main(String[] arg)> > {> > > try> {> > > System.out.println(> > 'Trying to initialize'> > +> ' a N with value'> > +> ' Integer.MAX_VALUE + 1'> );> > > // Try to store value Integer.MAX_VALUE + 1> > int> N = Integer.MAX_VALUE +> 1> ;> > > // Print the value of N> > System.out.println(> 'N = '> + N);> > }> > catch> (Exception e) {> > System.out.println(e);> > }> > }> }> |
Produzione:
Trying to initialize a N with value Integer.MAX_VALUE + 1 N = -2147483648Intero.MIN_VALUE
Integer.MIN_VALUE è una costante in Classe intera del pacchetto java.lang che specifica che memorizza il valore minimo possibile per qualsiasi variabile intera in Java. Il valore reale di questo è
-2^31 = -2147483648
Esempio 3:
// Java program to show> // the value of Integer.MIN_VALUE> > class> GFG {> > > // Driver code> > public> static> void> main(String[] arg)> > {> > > // Print the value of Integer.MIN_VALUE> > System.out.println(> 'Integer.MIN_VALUE = '> > + Integer.MIN_VALUE);> > }> }> |
Produzione:
Integer.MIN_VALUE = -2147483648
Qualsiasi variabile intera non può memorizzare alcun valore al di sotto di questo limite. In questo modo, la memoria traboccherà e il valore diventerà positivo.
Esempio 2: Tentativo di inizializzare un valore variabile Integer.MIN_VALUE – 1
// Java program to show what happens when> // a value less than Integer.MIN_VALUE> // is stored in an int variable> > class> GFG {> > > // Driver code> > public> static> void> main(String[] arg)> > {> > > try> {> > > System.out.println(> > 'Trying to initialize'> > +> ' a N with value'> > +> ' Integer.MIN_VALUE - 1'> );> > > // Try to store value Integer.MIN_VALUE - 1> > int> N = Integer.MIN_VALUE -> 1> ;> > > // Print the value of N> > System.out.println(> 'N = '> + N);> > }> > catch> (Exception e) {> > System.out.println(e);> > }> > }> }> |
Produzione:
Trying to initialize a N with value Integer.MIN_VALUE - 1 N = 2147483647