logo

Maksimalna veličina Java niza

U ovom odjeljku ćemo raspravljati koja je najveća veličina niza u Javi.

U Java , a Niz može se smatrati nizom znakova, a niz znakova nazvati nizom. Klasa String predstavlja niz znakova. Ne možemo promijeniti niz nakon što je kreiran. String objekti se ne mogu dijeliti jer jesu nepromjenjiv . Na primjer, razmotrite sljedeći niz:

hiba bukhari
 String str='javatpoint'; 

Gornji niz je ekvivalentan sljedećem:

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

Klasa String pruža metodu length() koja određuje duljinu Stringa. Sintaksa metode je sljedeća:

 public int length() 

Metoda vraća duljinu niza. The duljina niza jednak je broju Unicode jedinice u nizu. Java platforma koristi UTF-16 reprezentaciju u nizovima znakova (svaki znak ima dva bajta), klasama String i StringBuffer. U ovom prikazu, dopunski znakovi predstavljeni su kao par vrijednosti znakova, prvi iz raspona visokih surogata (uD800-uDBFF), drugi iz raspona niskih surogata (uDC00-uDFFF).

Metoda vraća duljinu koja je tipa int. Dakle, maksimalna veličina niza jednaka je rasponu tipa podataka cijelog broja. Maksimalna duljina koju bi vratila metoda bila bi Integer.MAX_VALUE.

Veličina int u Javi je 4 bajta (uključujući bit s predznakom, tj. MSB). Raspon tipa podataka cijelog broja je -231do 231-1 (-2147483648 do 2147483647). Zapamtite da ne možemo koristiti negativne vrijednosti za indeksiranje. Indeksiranje se vrši unutar maksimalnog raspona. To znači da ne možemo pohraniti 2147483648th lik. Stoga je maksimalna duljina Stringa u Javi 0 do 2147483647 . Dakle, možemo imati String duljine 2,147,483,647 znakova, teoretski.

Pronađimo maksimalnu duljinu niza pomoću Java programa.

StringMaxSize.java

.04 kao razlomak
 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;>

Izlaz:

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