In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "why high resources are occupied in Java". In daily operation, I believe that many people have doubts about why high resources are occupied in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "why high resources are occupied in Java". Next, please follow the editor to study!
In Java, a string object is immutable, meaning that once it is created, you can no longer change it. So when we concatenate the strings, we create a new string, and the old one is marked by the garbage collector.
If we process millions of strings, then we will generate millions of additional strings to be processed by the garbage collector.
The underlying layer of the virtual machine performs a number of operations when splicing strings. The most direct point operation (dot operator) for concatenating strings is the String#concat (String) operation.
Public String concat (String str) {int otherLen = str.length (); if (otherLen = = 0) {return this;} int len = value.length; char buf [] = Arrays.copyOf (value, len + otherLen); str.getChars (buf, len); return new String (buf, true);} public static char [] copyOf (char [] original, int newLength) {char [] copy = new char [newLength] System.arraycopy (original, 0, copy, 0, Math.min (original.length, newLength)); return copy;} void getChars (char dst [], int dstBegin) {System.arraycopy (value, 0, dst, dstBegin, value.length);}
You can see that an array of characters is created and the length is the sum of the existing and concatenated character lengths. Their values are then copied to the new character array. Use this character array to create a String object and return it.
So there are a lot of these operations, and if you do the math, you'll find that it's O (n ^ 2) complexity.
To solve this problem, we use the StringBuilder class. It's like a mutable String class. The splicing method helps us avoid unnecessary copying. It has the complexity of O (n), which is much better than O (n ^ 2).
However, Java 8 uses StringBuilder concatenation strings by default.
Documentation for Java 8:
To improve the performance of string concatenation, the Java compiler can use StringBuffer classes or similar techniques to reduce the creation of intermediate String objects when using evaluation expressions.
The Java compiler handles this situation:
Public class StringConcatenateDemo {public static void main (String [] args) {String str = "Hello"; str + = "world";}}
The above code is compiled into the following bytecode:
Public class StringConcatenateDemo {public StringConcatenateDemo (); Code: 0: aload_0 1: invokespecial # 1 / / Method java/lang/Object. "": () V 4: return public static void main (java.lang.String []) Code: 0: ldc # 2 / / String Hello 2: astore_1 3: new # 3 / / class java/lang/StringBuilder 6: dup 7: invokespecial # 4 / / Method java/lang/StringBuilder. ": () V 10: aload_1 11: Invokevirtual # 5 / / Method java/lang/StringBuilder.append: (Ljava/lang/String ) Ljava/lang/StringBuilder; 14: ldc # 6 / / String world 16: invokevirtual # 5 / / Method java/lang/StringBuilder.append: (Ljava/lang/String;) Ljava/lang/StringBuilder; 19: invokevirtual # 7 / / Method java/lang/StringBuilder.toString: () Ljava/lang/String; 22: astore_1 23: return}
As you can see in these bytecodes, StringBuilder is used. So we no longer need to use the StringBuilder class in Java 8.
At this point, the study on "why high resources are occupied in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.