logo

Dimensione massima stringa Java

In questa sezione discuteremo qual è la dimensione massima della stringa in Java.

In Giava , UN Corda può essere considerato come un array di caratteri e la sequenza di caratteri chiamata stringa. La classe String rappresenta stringhe di caratteri. Non possiamo modificare la stringa una volta creata. Gli oggetti stringa non possono essere condivisi perché lo sono immutabile . Consideriamo ad esempio la seguente stringa:

valore Java dell'enum
 String str='javatpoint'; 

La stringa sopra è equivalente a:

 char ch[] = {'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't'}; String str = new String(ch); 

La classe String fornisce il metodo length() che determina la lunghezza della String. La sintassi del metodo è la seguente:

 public int length() 

Il metodo restituisce la lunghezza della stringa. IL lunghezza della corda è uguale al numero di Unità Unicode nella corda. La piattaforma Java utilizza la rappresentazione UTF-16 negli array di caratteri (ogni carattere occupa due byte), nelle classi String e StringBuffer. In questa rappresentazione, i caratteri supplementari sono rappresentati come una coppia di valori char, il primo dall'intervallo di surrogati alti (uD800-uDBFF), il secondo dall'intervallo di surrogati bassi (uDC00-uDFFF).

Il metodo restituisce la lunghezza che è di tipo int. Pertanto, la dimensione massima della stringa è uguale all'intervallo del tipo di dati intero. La lunghezza massima restituita dal metodo sarebbe Integer.MAX_VALUE.

La dimensione di int in Java è 4 byte (incluso un bit con segno, ovvero MSB). L'intervallo del tipo di dati intero è -231a 231-1 (da -2147483648 a 2147483647). Ricorda che non possiamo utilizzare valori negativi per l'indicizzazione. L'indicizzazione viene eseguita entro l'intervallo massimo. Significa che non possiamo memorizzare il file 2147483648th carattere. Pertanto, la lunghezza massima di String in Java è da 0 a 2147483647 . Quindi, teoricamente, possiamo avere una stringa con una lunghezza di 2.147.483.647 caratteri.

Troviamo la lunghezza massima della stringa tramite un programma Java.

StringMaxSize.java

 import java.util.Arrays; public class StringMaxSize { public static void main(String args[]) { for (int i = 0; i <1000; i++) { try integer.max_value is a constant that stores the maximum possible value for any integer variable char[] array="new" char[integer.max_value - i]; assign specified data to each element arrays.fill(array, 'a'); creating constructor of string class and parses an into it str="new" string(array); determines print length system.out.println(str.length()); } catch (throwable e) returns detail message this throwable system.out.println(e.getmessage()); prints system.out.println('last: ' + (integer.max_value i)); i); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/05/java-string-max-size.webp" alt="Java String Max Size"> <h4>Note: We have not shown the complete output because the output is too long to show.</h4> <p>In the above example, we have used a for loop that executes 1000 times. Inside the try block, we have created an array of <strong>Integer.MAX_VALUE-i</strong> . After that, we have invoked the fill() method of the Arrays class. It assigns the specified data type value to each element of the specified range of the specified array.</p> <p>Inside the catch block, we caught the exception (if any) thrown by the fill() method and the <strong>getMessage()</strong> method prints the message related to the exception.</p> <p>Each character takes two bytes because Java stores string as UTF-16 codes.</p> <p>Whether you are appending strings directly or using a StringBuilder (much better), you will occasionally need twice as much memory: one to store the existing string and one to store the new string/buffer when it needs to be expanded.</p> <p>If we try to insert the value beyond the limit upon doing so, the memory gets overflow and the value that we get will be negative. For example, consider the following program:</p> <p> <strong>StringSizeBeyondLimit.java</strong> </p> <pre> public class StringSizeBeyondLimit { public static void main(String[] arg) { try { System.out.println( &apos;Trying to initialize&apos; + &apos; a n with value&apos; + &apos; Integer.MAX_VALUE + 1&apos;); // Try to store value Integer.MAX_VALUE + 1 int n = Integer.MAX_VALUE + 1; // Print the value of N System.out.println(&apos;n = &apos; + n); } catch(Exception e) { System.out.println(e); } } } </pre> <p> <strong>Output:</strong> </p> <pre> Trying to initialize n with value Integer.MAX_VALUE + 1 n = -2147483648 </pre> <hr></1000;>

Produzione:

centos contro redhat
 Trying to initialize n with value Integer.MAX_VALUE + 1 n = -2147483648