In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
2025-11-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article to share with you is about Java String definition literal maximum length is how much, Xiaobian think quite practical, so share to everyone to learn, I hope you can read this article after some harvest, not much to say, follow Xiaobian to see it.
Java String object bottom layer is a character array storage, theoretically char[] maximum length is the maximum value of int
Thinking:
First, String literal constants are maintained by the String class and can be determined at compile time (see String constant pool for details). Thus, if there is a maximum length for String literal constants (assumed for now) and we use literal constants that exceed this limit, then the compiler can give an error message during compilation. Therefore, we can use IO stream to generate Java file, the content of the file is to declare a String object, and then use literal constant assignment, according to the dynamic compilation results, adjust the length of the literal constant, and finally get the maximum length value of the literal constant
Draw conclusions based on the following code (code from the book Java Deep Parsing: 36 Topics to Dialyse Java Essence):
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStream; import javax.tools.JavaCompiler; import javax.tools.ToolProvider; public class LiteralLength { public static void main(String[] args) throws Exception { String fileName = "D:/Literal.java"; StringBuilder prefix = new StringBuilder(); prefix.append ("public class Literal{ String s = \""); int low = 0; int high = 100_0000; int mid =(low + high)/2; StringBuilder literal = new StringBuilder(high); int result; String ch = "A"; JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();//custom error output stream err = new OutputStream() { @Override public void write(int b) throws IOException { } }; int max = 0; for (int i = 0; i < mid; i++) { literal.append(ch); } while(low
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.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.