logo

Mida màxima de la cadena Java

En aquesta secció, parlarem quina és la mida màxima de la cadena a Java.

En Java , a Corda es pot considerar com una matriu de caràcters, i la seqüència de caràcters s'anomena cadena. La classe String representa cadenes de caràcters. No podem canviar la cadena un cop creada. Els objectes de cadena no es poden compartir perquè sí immutable . Per exemple, considereu la cadena següent:

classe de cadena java
 String str='javatpoint'; 

La cadena anterior és equivalent a:

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

La classe String proporciona el mètode length() que determina la longitud de la cadena. La sintaxi del mètode és la següent:

 public int length() 

El mètode retorna la longitud de la cadena. El longitud de la corda és igual al nombre de Unitats Unicode a la corda. La plataforma Java utilitza la representació UTF-16 en matrius de caràcters (cada caràcter pren dos bytes), les classes String i StringBuffer. En aquesta representació, els caràcters suplementaris es representen com un parell de valors de caràcters, el primer del rang de substituts alts, (uD800-uDBFF), el segon del rang de substituts baixos (uDC00-uDFFF).

El mètode retorna la longitud que és de tipus int. Per tant, la mida màxima de la cadena és la mateixa que l'interval del tipus de dades enter. La longitud màxima que retornaria el mètode seria Integer.MAX_VALUE.

La mida de int a Java és de 4 bytes (inclou un bit signat, és a dir, MSB). L'interval del tipus de dades enter és -231a 231-1 (-2147483648 a 2147483647). Recordeu que no podem utilitzar valors negatius per a la indexació. La indexació es fa dins del rang màxim. Vol dir que no podem emmagatzemar el 2147483648th personatge. Per tant, la longitud màxima de String a Java és 0 al 2147483647 . Per tant, podem tenir una cadena amb una longitud de 2.147.483.647 caràcters, teòricament.

Trobem la longitud màxima de la cadena mitjançant un programa Java.

StringMaxSize.java

com llegir el fitxer csv en 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;>

Sortida:

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