logo

Java eilutės maksimalus dydis

Šiame skyriuje aptarsime koks yra maksimalus „Java“ eilutės dydis.

Į Java , a Styga gali būti laikomas simbolių masyvu, o simbolių seka vadinama eilute. Styginių klasė reiškia simbolių eilutes. Negalime pakeisti eilutės, kai ji bus sukurta. Styginių objektai negali būti bendrinami, nes jie yra nekintamas . Pavyzdžiui, apsvarstykite šią eilutę:

sąrašą rūšiuoti pagal java
 String str='javatpoint'; 

Aukščiau pateikta eilutė yra lygiavertė:

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

Styginių klasė pateikia ilgio () metodą, kuris nustato eilutės ilgį. Metodo sintaksė yra tokia:

 public int length() 

Metodas grąžina eilutės ilgį. The stygos ilgis yra lygus skaičiui Unikodo vienetai stygoje. Java platforma naudoja UTF-16 atvaizdavimą char masyvuose (kiekvienas simbolis užima du baitus), String ir StringBuffer klasėse. Šiame pavyzdyje papildomi simboliai pateikiami kaip simbolių reikšmių pora, pirmasis iš didelio pakaitinių ženklų diapazono (uD800-uDBFF), antrasis iš mažų pakaitalų diapazono (uDC00-uDFFF).

Metodas grąžina ilgį, kurio tipas yra int. Taigi maksimalus eilutės dydis yra toks pat kaip sveikojo skaičiaus duomenų tipo diapazonas. Didžiausias ilgis, kurį grąžintų metodas, būtų Integer.MAX_VALUE.

log4j

Int dydis Java yra 4 baitai (įskaitant pasirašytą bitą, ty MSB). Sveikųjų skaičių duomenų tipo diapazonas yra -231iki 231-1 (nuo 2147483648 iki 2147483647). Atminkite, kad indeksuodami negalime naudoti neigiamų verčių. Indeksavimas atliekamas maksimaliu diapazonu. Tai reiškia, kad negalime saugoti 2147483648th charakteris. Todėl maksimalus „Java“ eilutės ilgis yra 0 iki 2147483647 . Taigi, teoriškai galime turėti 2 147 483 647 simbolių eilutę.

Raskime maksimalų eilutės ilgį per 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;>

Išvestis:

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