logo

Java String Maks Size

I denne delen vil vi diskutere hva er maksimal størrelse på strengen i Java.

I Java , a String kan betraktes som en rekke tegn, og sekvensen av tegn kalles en streng. String-klassen representerer tegnstrenger. Vi kan ikke endre strengen når den først er opprettet. Strengobjekter kan ikke deles fordi de er det uforanderlig . Tenk for eksempel på følgende streng:

pandas standardavvik
 String str='javatpoint'; 

Strengen ovenfor tilsvarer:

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

String-klassen gir lengde()-metoden som bestemmer lengden på strengen. Syntaksen til metoden er som følger:

 public int length() 

Metoden returnerer lengden på strengen. De lengden på strengen er lik antall Unicode-enheter i strengen. Java-plattformen bruker UTF-16-representasjonen i char-arrays (hvert tegn tar to byte), String- og StringBuffer-klasser. I denne representasjonen er tilleggstegn representert som et par char-verdier, den første fra high-surrogates-området, (uD800-uDBFF), den andre fra low-surrogates-området (uDC00-uDFFF).

Metoden returnerer lengden som er av typen int. Så strengens maksimale størrelse er den samme som rekkevidden av heltallsdatatypen. Den maksimale lengden som vil bli returnert av metoden vil være heltall.MAX_VALUE.

Størrelsen på int i Java er 4 byte (inkludert en signert bit, dvs. MSB). Området for heltallsdatatypen er -231til 231-1 (-2147483648 til 2147483647). Husk at vi ikke kan bruke negative verdier for indeksering. Indekseringen gjøres innenfor det maksimale området. Det betyr at vi ikke kan lagre 2147483648th karakter. Derfor er den maksimale lengden på streng i Java 0 til 2147483647 . Så vi kan ha en streng med lengden på 2 147 483 647 tegn teoretisk.

La oss finne maksimal lengde på strengen gjennom et Java-program.

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;>

Produksjon:

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