Java String, StringBuilder, StringBuffer, Hashtable, HashM

Source: Internet
Author: User

The question is a simple applet, as shown below: [java] public class Test1 {public static void main (String args []) {String s = new String ("Hello"); System. out. println (s); foo (s); System. out. println (s);} public static void foo (String s) {s = new String ("World");} asked the program what it will output twice. The first one must output "Hello ". The key is the second one. Some friends with poor foundation may be put down, but they will not be able to get a solid foundation. The second output is "Hello ". Everything is fine. The interviewer glared in joy and said, "Well, it's good. It won't change, right. Because the String type is immutable, immutable type cannot be changed ." "Immutable" means that no method in the String class can change the object state. This is problematic. It is true that the program outputs "Hello" twice, but the reason is not the immutable feature of String. Do not believe to change to a "mutable", such as StringBuilder or StringBuffer. [Java] public class Test2 {public static void main (String args []) {StringBuilder s = new StringBuilder ("Hello"); System. out. println (s); foo (s); System. out. println (s);} public static void foo (StringBuilder s) {s = new StringBuilder ("World") ;}} what about this time? Will "Hello" be output first, and then "World" be output? No, it is still "Hello" twice ". This problem is not related to the immutable feature of String. Whether immutable type or general type, the first and second objects in the main method of the program written in this way must be the same. The real reason is that the parameter transfer mechanism in Java is "pass by value ). Although other variables except the basic numeric type in Java are referenced, it is another thing. The semantics of variables ("Reference" or "value") and the mechanism for passing parameters through functions are two orthogonal concepts. Among various programming languages, the most common parameter transfer method is pass by value and pass by reference. For example, function parameters in C and Java are passed by value, while C ++ and C # both support passing by value and by reference. The difference between C and Java is that C is a value-based transfer, while Java is a value-based transfer of reference types (except for basic numeric types ). The biggest feature of passing parameters by value is that the function cannot perceive the "Real Variable" outside of the modifications made to the "parameter variable" itself. However, for StringBuilder, we should at least have a way to change it, for example, [java] public class Test3 {public static void main (String args []). {StringBuilder s = new StringBuilder ("Hello"); System. out. println (s); foo (s); System. out. println (s);} public static void foo (StringBuilder s) {s. replace (0, s. length (), "World") ;}} is it different? Although the parameter variable itself is passed by value, this time we are not interested in the variable itself, we do not change the variable itself, but directly modify the object it references through it. This time, the Java language "almost everything is referenced" feature takes effect. The program outputs "Hello" for the first time and "World" for the second time ". A friend who is familiar with C may immediately think that it is similar to using a pointer in C to modify the content it points. In the previous String version, we cannot write a variable version. Why ?...... "Well, it's good. Because String is of the immutable type ......", This is true. -- We can see that the previous saying "there is no relation to a dime" is not all at all. Because of immutable, we cannot write a "variable" program like Test3 for the String. In this case, there should be a "binary relationship. "Pass reference by value" is like "An lvalue with rvalue reference type" in C ++ 11. At first glance, it is quite difficult, but if you think about it carefully, we can deepen our understanding of language by making the orthogonal things "dust-soil. By the way, do you know the differences between java. lang. StringBuilder and java. lang. StringBuffer? Many interviewers like "by the way" to ask this question. If you get a correct answer, you will not receive any extra points. If you cannot answer the question, you will be deducted at least the lowest score. :( Java. lang. stringBuilder and java. lang. difference of StringBuffer: StringBuffer is synchronized, StringBuilder is not. stringBuilder is faster than StringBuffer because it's not synchronized. it seems that it is not a good habit to use StringBuffer in the past. We will use StringBuilder in the future !!! This also reminds me of another classic question: What is the difference between Hashtable and HashMap? Hashtable is synchronized, whereas HashMap is not. this makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones. hashtable does not allow null keys or values. hashMap allows one null key and any number ofnull values. one of HashMap's subclasses is your HashMap, so in the event that you 'd want predictable iteration order (which is insertion order by default), you cocould easily swap out the HashMap for aLinkedHashMap. this wouldn't be as easy if you were using Hashtable. since synchronization is not an issue for you, I 'd recommend HashMap. if synchronization becomes an issue, you may also look at ConcurrentHashMap. in short, unsynchronized is faster, but considering the concept of Hashtable is much more efficient than HashMap in interviews, Hashtable is used for interview questions. in project development, I have always used HashMap.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.