In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the principle of Java memory allocation". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Some examples of String constant Pool problem
Here is a comparative analysis and understanding of several common examples:
String a = "A1"; String b = "a" + 1; System.out.println ((a = = b)); / / result = true String a = "atrue"; String b = "a" + "true"; System.out.println ((a = = b)); / / result = true String a = "a3.4"; String b = "a" + 3.4; System.out.println ((a = = b)); / / result = true
Analysis: JVM for string constant "+" sign connection, the program compilation time, JVM constant string "+" connection will be optimized for the connected value, for example, "a" + 1, optimized by the compiler in class is already A1. The value of its string constant is determined at compile time, so the final result of the above program is true.
String a = "ab"; String bb = "b"; String b = "a" + bb; System.out.println ((a = = b)); / / result = false
Analysis: JVM for string references, because in the string "+" connection, there is a string reference, and the value of the reference can not be determined during the program compilation time, that is, "a" + bb can not be optimized by the compiler, only in the run time of the program to dynamically allocate and assign the connected new address to b. So the result of the above program is false.
String a = "ab"; final String bb = "b"; String b = "a" + bb; System.out.println ((a = = b)); / / result = true
Analysis: unlike the * * in [3], the bb string is modified by final. For variables modified by final, it is parsed into a local copy of the constant value at compile time, stored in its own constant pool or embedded in its byte stream. So the effect of "a" + bb is the same as "a" + "b" at this time. So the result of the above program is true.
String a = "ab"; final String bb = getBB (); String b = "a" + bb; System.out.println ((a = = b)); / / result = false private static String getBB () {return "b";}
Analysis: JVM for string reference bb, its value can not be determined at compile time, only after the program runtime calls the method, the method return value and "a" to dynamically connect and assign the address to b, so the result of the above program is false.
We can know from the above four examples:
String s = "a" + "b" + "c"
Is equivalent to
String s = "abc"; String a = "a"; String b = "b"; String c = "c"; String s = a + b + c
This is different, and the end result is:
StringBuffer temp = new StringBuffer (); temp.append (a) .append (b) .append (c); String s = temp.toString ()
From the above analysis results, it is not difficult to infer that String uses the join operator (+) to analyze the reasons for its inefficiency, such as this code:
Public class Test {public static void main (String args []) {String s = null; for (int I = 0; I < 100; iTunes +) {s + = "a";}
Every time you do +, it produces a StringBuilder object, which is then append and discarded. The next time the loop arrives, it regenerates a StringBuilder object, and then append the string, so the loop ends. If we directly use StringBuilder objects for append, we can save N-1 time to create and destroy objects. So for applications that want to concatenate strings in a loop, they usually use StringBuffer or StringBulider objects for append operations.
Understanding and analysis of intern methods of String objects:
Public class Test4 {private static String a = "ab"; public static void main (String [] args) {String S1 = "a"; String S2 = "b"; String s = S1 + S2; System.out.println (s = = a); / / false System.out.println (s.intern () = = a); / / true}}
The JAVA used here is a constant pool problem. For the s1+s2 operation, a new object is actually recreated in the heap. S holds the contents of the new object in the heap space, so that the values of s and an are not equal. When you call the s.intern () method, you can return the address value of s in the constant pool, because the value of an is stored in the constant pool, so the values of s.intern and an are equal.
This is the end of the content of "what is the principle of Java memory allocation". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.