logo

Taille maximale de la chaîne Java

Dans cette section, nous discuterons quelle est la taille maximale de la chaîne en Java.

Dans Java , un Chaîne peut être considéré comme un tableau de caractères, et la séquence de caractères appelée une chaîne. La classe String représente des chaînes de caractères. Nous ne pouvons pas modifier la chaîne une fois qu'elle est créée. Les objets chaîne ne peuvent pas être partagés car ils sont immuable . Par exemple, considérons la chaîne suivante :

décodage javascript base64
 String str='javatpoint'; 

La chaîne ci-dessus équivaut à :

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

La classe String fournit la méthode length() qui détermine la longueur de la chaîne. La syntaxe de la méthode est la suivante :

 public int length() 

La méthode renvoie la longueur de la chaîne. Le longueur de la chaîne est égal au nombre de Unités Unicode dans la chaîne. La plate-forme Java utilise la représentation UTF-16 dans les tableaux de caractères (chaque caractère prend deux octets), les classes String et StringBuffer. Dans cette représentation, les caractères supplémentaires sont représentés sous la forme d'une paire de valeurs de caractères, la première de la plage des substituts élevés (uD800-uDBFF), la seconde de la plage des substituts faibles (uDC00-uDFFF).

La méthode renvoie la longueur qui est de type int. Ainsi, la taille maximale de la chaîne est la même que la plage du type de données entier. La longueur maximale qui serait renvoyée par la méthode serait Integer.MAX_VALUE.

La taille de int en Java est de 4 octets (y compris un bit signé, c'est-à-dire MSB). La plage du type de données entier est -231à 231-1 (-2147483648 à 2147483647). N'oubliez pas que nous ne pouvons pas utiliser de valeurs négatives pour l'indexation. L'indexation se fait dans la plage maximale. Cela signifie que nous ne pouvons pas stocker le 2147483648ème personnage. Par conséquent, la longueur maximale de String en Java est 0 au 2147483647 . Ainsi, nous pouvons théoriquement avoir une chaîne d’une longueur de 2 147 483 647 caractères.

connectivité Java

Trouvons la longueur maximale de la chaîne via un programme 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;>

Sortir:

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