recent work, a more normal code String a = "hello" + "world"; was converted into a new StringBuilder (). append ("hello"). append ("world"); time relatively doubts benefits of doing so, and later to the Internet to find a lot after it became clear that the strings in Java reside mechanisms, then what is it reside?
the name suggests, is resident in memory retention (in Java, we usually say where the object resides is resident pool, but it is also part of the memory), it not only exists in Java, also exists in C # . So I wrote a few examples to explain what resides in Java strings:
1 | |
After executing the above code String a = "abc" This is a time in memory to create a value of type String object abc. When executing under a code, that String b = "abc" when, java will first go to reside inside the pool to find whether there is abc string object, if there is reference to the implementation of that object let b, if not the newly created a resident and store it in the pool. Therefore, it is easy to understand, when the program execution to the third sentence when it will return true, we know that == in java compare the object references pointing to the first address of the object's memory is the same, and a and b point to the the same object, it will return true. Continue to go down, when the program execution to a String c = new String ("abc") this sentence, java do include: checking whether the object in the string abc resides in the pool, if there put it as value, and then created on the heap a String object into the heap (we all know that the object is placed in the java heap, object references are stored in the stack). So this sentence may actually create two objects (if abc already resides in the pool, and it just creates an object in the heap). Same time, through new String () string object is created out of the resident will not be placed in the pool. You may be thinking, is there a way for me to put the objects created on the heap resides in the pool go? The answer is yes! java provides a method called intern (), if you perform c.intern (), will be the first point of the object into the c resides in the pool, and then returns a pointer to the object. So, the following code will output what?
1 | |
of course, is true! But it's still worth saying about the implementation process, and focus on b.intern (); these words on. After the above you may want to explain the process should be the first to create a value abc heap String object, then the object into the resident pool. So if the value resides in the pool already exists abc string object of it? So b.intern will return directly to objects that reside in the pool, so here will return true. Continue down the execution, System.out.println (a == b); returns false, because in the implementation b.intern (); these words, when, in fact, is a direct return objects that reside in the pool, so b pointing heap of the original object has no effect, so a == b returns false.
I passed the above simple example to explain the string of java resident, now back to the article at the beginning of the doubts, why use a StringBuilder instead of simply using the "+" to concatenate strings do? After the above explanation, you might guess StringBuilder string with a resident, and the "+" is not. Congratulations, you got it, add 10 points. But you may not know that using the "+" when the tricky places where. Continue to look down.
reason is that using the + connection string generated each time a new object, but also in the memory on the heap, and the heap memory is slower (relatively speaking), then again a lot of the connection string is undesirable direct + , of course, requires a high efficiency approach. Java provides the StringBuffer and StringBuilder is to solve this problem. The difference is that the former while the latter is a non-thread-safe thread-safe. It prompted me to write this blog cause of the problem is found. In addition, it is worth noting is that reside pool is not collected by the GC, it will remain in the program is running.
Finally, I would like to say point the way, look at the following program:
1 | |
c == d output true, because c and d are string constants, and their value is determined at compile time. And all that relates to the referenced places are determined at run time values, all of the following will be full output to false.
没有评论:
发表评论