Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the maximum character length of String?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what is the maximum character length of String". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the maximum character length of String"?

The String class is arguably the most frequently used class in Java, and it is no stranger to beginners who are new to Java, because for Java programs, the main method uses an array of String types as a parameter (String [] args). How long can the String string be for such a frequently used class? 100,000 characters? 1 million characters? Or is it infinite?

To figure out the maximum length of String, you should first understand the internal implementation of the String class. In the String class, a character array is used to maintain the character sequence, which is declared as follows:

Private final char value []

That is, the maximum length of the String depends on the maximum length of the character array, and we know that when specifying the length of the array, you can use the byte, short, char, int types, but not the long type. That is to say, the maximum length of the array is the maximum value of type int, that is, 0x7fffffff, and the decimal system is 2147483647. Similarly, this is the maximum number of characters that String can hold.

Also, let's take a look at the java.lang.String#length () source code:

Public int length () {return value.length;}

You can also see that the return value of the length method that gets the length of the String object is of type int, not of type long.

However, this maximum value is only a value that can be achieved in theory, and in our practical use, the maximum length obtained is generally smaller than the theoretical value. Let's write the simplest program to see.

/ * * @ author wupx * @ date 2020-01-13 * / public class StringTest {public static void main (String [] args) {char [] c = new char [Integer.Max _ VALUE];}}

Running this program, under normal circumstances, produces the following error:

Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit at test.StringTest.main (StringTest.java:9)

The cause of this error is a memory overflow, that is, the system cannot allocate such a large amount of memory space. Calculate that one char type takes up 2 bytes, and 2147483647 char types are 4294967294 bytes, which is close to the size of 4GB, so it's not surprising to fail to apply for such a large chunk of continuous memory space.

So, how many character arrays can our computers bear? it has to do with many factors, such as software and hardware, and we can write programs to get an approximation of the largest character array that can be applied for.

/ * * @ author wupx * @ date 2020-01-13 * / public class StringTest {public static void main (String [] args) {for (int I = 0; I < 100; iTunes +) {int len = Integer.MAX_VALUE-I; try {char [] ch = new char [len]; System.out.println ("len:" + len + "OK") } catch (Error e) {System.out.println ("len:" + len + "" + e);}

The running results are as follows:

Len: 2147483647 java.lang.OutOfMemoryError: Requested array size exceeds VM limitlen: 2147483646 java.lang.OutOfMemoryError: Requested array size exceeds VM limitlen: 2147483645 OKlen: 2147483644 OKlen: 2147483643 OK

From the running results, we can see that the maximum length of String is Integer.MAX_VALUE-2 or 2 ^ 31-3.

Thank you for reading, the above is the content of "what is the maximum character length of String". After the study of this article, I believe you have a deeper understanding of the problem of how much the maximum character length of String is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report