"Reprint" Questions about Java String, StringBuilder, StringBuffer, Hashtable, HashMap

Source: Internet
Author: User

ref:http://blog.csdn.net/fightforyourdream/article/details/15333405

The topic is a simple applet, like this:
[Java] View plaincopy
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");
}
}

Ask the program two times each will output what.
The first one definitely outputs "Hello". The key is the second, the individual foundation of the friend may be down, but a little more solid foundation will not. The second output is also a "Hello".
Here it is. The interviewer gave the light of joy and praised the way: "Well, good." It's not going to change, right. Because string is a immutable type, the immutable type cannot be changed. "Immutable", he says, means that there is no one method in the string class that alters the state of an object.

That's a problem. It is true that the program output is "Hello" two times, but not because of the immutable feature of string. Do not believe in a "mutable" test, such as StringBuilder or StringBuffer.

[Java] View plaincopy
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 you output "Hello" and then output "world"? No, it's still two times "Hello".
Suffice to say that this problem is not related to the immutable characteristic of string. Whether it's a immutable type or a generic type, the program written in this way is definitely the same in the main method, before and after two objects.

The real reason is that the parameter passing mechanism of the Java language is "pass by value". While Java has references to other variables in addition to the base numeric types, that is another matter. The semantics of a variable ("reference" or "value") are the two orthogonal concepts that are used to pass parameters to a function.
In various programming languages, the most common way to pass parameters is to pass by value and pass by reference (pass by reference) two. For example, function parameters in C and Java are passed by value, while C + + and C # support both pass-by-value and pass-by-reference. The difference between C and Java is that C is passed by value for a value type, whereas Java is a value pass for a reference type (except for a basic numeric type).
The most important feature of parameter-by-value is that the "real parametric" is not perceived by the inside of the function for the modification of "shape parametric" itself.

However, for StringBuilder we have at least a way to make it change, like this:

[Java] View plaincopy
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");
}
}


Do you feel the difference? 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 through it directly modify the object it refers to. This time, the Java language "almost everything is quoted" features a function, the first time the program output "Hello", the second output "world". A friend familiar with C may immediately associate it with the following: It is much like the C language that modifies what it points to by pointers.
And the previous string version of the program, we could not write a corresponding variable version. Why is it? ... "Well, good. Because string is the immutable type ... ", this time it's true. It is not true that the "relationship without a dime" was previously said. Because of immutable, we couldn't write a "mutable" program like TEST3 for string, so "the relationship between two cents" should be there.

"Pass reference by value" is like "an lvalue with Rvalue reference type" in C + + 11, at first glance quite round, but as long as you think carefully, let the orthogonal thing "dust return to Earth", can deepen the understanding of language.
By the way, do you know the difference between Java.lang.StringBuilder and Java.lang.StringBuffer? Many interviewers like to "by the way" to ask this, and the correct answer will not add points, do not come up but will deduct points, at least buckle impression points. :(

The difference between Java.lang.StringBuilder and Java.lang.StringBuffer:
StringBuffer is synchronized, StringBuilder are not.
StringBuilder is faster than stringbuffer because it's not synchronized.

It seems that used to like to use StringBuffer is not a good habit, later to use more StringBuilder!!!



This also reminds me of another classic question: What is the difference between Hashtable and HashMap?
Hashtable is synchronized, whereas HashMap are not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchroni Zed 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 linkedhashmap, so in the event it's want predictable iteration order (which is insert Ion order By default), you could easily swaps out the HashMap for Alinkedhashmap. This wouldn ' t is as easy if you were using Hashtable.
Since synchronization is a issue for you, I ' d recommend HashMap. If synchronization becomes an issue, the also look at Concurrenthashmap.


In short, unsynchronized to be faster, but in view of Hashtable this concept in the interview than HashMap to come more, so in the face of the test is still used Hashtable bar, in the project development I have always used HashMap.

REF:
http://blog.csdn.net/steedhorse/article/details/6892569
Http://stackoverflow.com/questions/355089/stringbuilder-and-stringbuffer-in-java
Http://vanillajava.blogspot.de/2013/04/why-synchronized-stringbuffer-was-never.html
Http://stackoverflow.com/questions/40471/differences-between-hashmap-and-hashtable

"Reprint" Questions about Java String, StringBuilder, StringBuffer, Hashtable, 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.