Người nghiện Internet chuyên nghiệp • Người đam mê trò chơi • Người sáng tạo công nghệ
Người nghiện Internet chuyên nghiệp • Người đam mê trò chơi • Người sáng tạo công nghệ

Đằng sau sự kết hợp của các chuỗi trong Java

Hãy cùng xem điều gì đang diễn ra bên trong cách nối các chuỗi trong Java!
Trang này đã được các thực tập sinh AI đầy nhiệt huyết của tôi dịch từ tiếng Anh để thuận tiện cho bạn. Các bạn vẫn đang trong quá trình học hỏi, nên có thể còn một vài lỗi nhỏ. Để có thông tin chính xác nhất, vui lòng tham khảo phiên bản tiếng Anh.
Trang chủ Blog Đằng sau sự kết hợp của các chuỗi trong Java

Xin lưu ý rằng bài đăng trên blog này được xuất bản vào tháng 1 năm 2011, vì vậy tùy thuộc vào thời điểm bạn đọc, một số phần có thể đã lỗi thời. Rất tiếc, tôi không thể luôn cập nhật đầy đủ các bài đăng này để đảm bảo thông tin luôn chính xác.

    As a web developer working with Java Servlets on a daily basis, I find my self often concatenating Strings all the time to create blocks of HTML, JavaScript, SQL (and so on) code to be used by the site. However, even the basic thing as concatenating Strings in Java could be more complicated than you thought when you looking under the hood.

    The big question

    In Java Strings are immutable, meaning once they are created they cannot be changed. So, if they cannot be changed and you want to modify the String, how do you do that the most efficient way without wasting resources?

    The different ways and how they work

    The three most common ways to concatenate a String is either by using the regular String concatenation (the "+=" operator), or using the StringBuilder (introduced in Java 5) or the StringBuffer (which is the synchronized version) classes.
    Each time you do a regular String concatenation, a new Java object will be created. Meaning the code "A" + "B" would leave three objects in the memory ("A", "B" and "AB"). However, this does not mean it's a total waste of resources. If you actually want the result of two Strings together (a+b), you might as well simply just do the regular concatenation instead of create a new StringBuilder/StringBuffer, because the Java compiler will most likely optimize it and you won't notice a difference in performance at all.
    But if you are planing to build up a large String in a loop and only needing the result at the very end, StringBuilder/StringBuffer is a better option, since regular concatenation has a O(N2) complexity while StringBuilder has O(N) in complexity, due to StringBuilder instead contains a sequence of characters.
    The bottom line is that String concatenation is always going to be less efficient than using a StringBuilder/StringBuffer. Even looking at the Javadoc of StringBuilder, StringBuffer is less efficient than StringBuilder:
    Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

    But don't sacrifice code readability

    Even thought using StringBuilder might be the most efficient way, don't sacrifice the readability of your code as it is also important. Typical code like:
    String message = new StringBuffer().append("Welcome ").append(name).append(". What do you want to do").toString();
    which can be written much more readably as:
    String message = "Welcome " + name + ". What do you want to do?";

    The Rule of thumb

    • Using the regular String concatenation is fine for a single statement
    • If you're looping to build up a large data of characters, go for StringBuffer.
    • Don't sacrifice code readability; getting confused or not understanding the code at all is often far more worse than decreased performance

    What I do

    Ever since I researched the different ways to concatenate Strings in Java, I always tend to automatically create a StringBuilder (over the regular String concatenation and StringBuffer) when dealing with large texts. Mostly because I know it's almost always the most efficient way to manipulate Strings. Even though it's unsynchronized it's very rare I need to share the information between threads.

    Được viết bởi Special Agent Squeaky. Đăng lần đầu ngày 08/01/2011. Cập nhật lần cuối ngày 08/01/2011.

    📺 Xem video mới nhất của Squeaky!

    Cách thêm phụ đề thời gian thực cho phát trực tiếp của bạn một cách đơn giản