<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## Chapter 9. General Programming(通用程序設計) ### Item 62: Avoid strings where other types are more appropriate(其他類型更合適時應避免使用字符串) Strings are designed to represent text, and they do a fine job of it. Because strings are so common and so well supported by the language, there is a natural tendency to use strings for purposes other than those for which they were designed. This item discusses a few things that you shouldn’t do with strings. 字符串被設計用來表示文本,它們在這方面做得很好。因為字符串是如此常見,并且受到 Java 的良好支持,所以很自然地會將字符串用于其他目的,而不是它們適用的場景。本條目討論了一些不應該使用字符串的場景。 **Strings are poor substitutes for other value types.** When a piece of data comes into a program from a file, from the network, or from keyboard input, it is often in string form. There is a natural tendency to leave it that way, but this tendency is justified only if the data really is textual in nature. If it’s numeric, it should be translated into the appropriate numeric type, such as int, float, or BigInteger. If it’s the answer to a yes-or-no question, it should be translated into an appropriate enum type or a boolean. More generally, if there’s an appropriate value type, whether primitive or object reference, you should use it; if there isn’t, you should write one. While this advice may seem obvious, it is often violated. 字符串是其他值類型的糟糕替代品。當一段數據從文件、網絡或鍵盤輸入到程序時,它通常是字符串形式的。有一種很自然的傾向是保持這種格式不變,但是這種傾向只有在數據本質上是文本的情況下才合理。如果是數值類型,則應將其轉換為適當的數值類型,如 int、float 或 BigInteger。如果是問題的答案,如「是」或「否」這類形式,則應將其轉換為適當的枚舉類型或布爾值。更一般地說,如果有合適的值類型,無論是基本類型還是對象引用,都應該使用它;如果沒有,你應該寫一個。雖然這條建議似乎很多余,但經常被違反。 **Strings are poor substitutes for enum types.** As discussed in Item 34, enums make far better enumerated type constants than strings. **字符串是枚舉類型的糟糕替代品。** 正如 [Item-34](/Chapter-6/Chapter-6-Item-34-Use-enums-instead-of-int-constants.md) 中所討論的,枚舉類型常量比字符串更適合于枚舉類型常量。 **Strings are poor substitutes for aggregate types.** If an entity has multiple components, it is usually a bad idea to represent it as a single string. For example, here’s a line of code that comes from a real system—identifier names have been changed to protect the guilty: **字符串是聚合類型的糟糕替代品。** 如果一個實體有多個組件,將其表示為單個字符串通常是一個壞主意。例如,下面這行代碼來自一個真實的系統標識符,它的名稱已經被更改,以免引發罪責: ``` // Inappropriate use of string as aggregate type String compoundKey = className + "#" + i.next(); ``` This approach has many disadvantages. If the character used to separate fields occurs in one of the fields, chaos may result. To access individual fields, you have to parse the string, which is slow, tedious, and error-prone. You can’t provide equals, toString, or compareTo methods but are forced to accept the behavior that String provides. A better approach is simply to write a class to represent the aggregate, often a private static member class (Item 24). 這種方法有很多缺點。如果用于分隔字段的字符出現在其中一個字段中,可能會導致混亂。要訪問各個字段,你必須解析字符串,這是緩慢的、冗長的、容易出錯的過程。你不能提供 equals、toString 或 compareTo 方法,但必須接受 String 提供的行為。更好的方法是編寫一個類來表示聚合,通常是一個私有靜態成員類([Item-24](/Chapter-4/Chapter-4-Item-24-Favor-static-member-classes-over-nonstatic.md))。 **Strings are poor substitutes for capabilities.** Occasionally, strings are used to grant access to some functionality. For example, consider the design of a thread-local variable facility. Such a facility provides variables for which each thread has its own value. The Java libraries have had a thread-local variable facility since release 1.2, but prior to that, programmers had to roll their own. When confronted with the task of designing such a facility many years ago, several people independently came up with the same design, in which clientprovided string keys are used to identify each thread-local variable: **字符串不能很好地替代 capabilities。** 有時,字符串用于授予對某些功能的訪問權。例如,考慮線程本地變量機制的設計。這樣的機制提供了每個線程都有自己的變量值。自 1.2 版以來,Java 庫就有了一個線程本地變量機制,但在此之前,程序員必須自己設計。許多年前,當面臨設計這樣一個機制的任務時,有人提出了相同的設計,其中客戶端提供的字符串鍵,用于標識每個線程本地變量: ``` // Broken - inappropriate use of string as capability! public class ThreadLocal { private ThreadLocal() { } // Noninstantiable // Sets the current thread's value for the named variable. public static void set(String key, Object value); // Returns the current thread's value for the named variable. public static Object get(String key); } ``` The problem with this approach is that the string keys represent a shared global namespace for thread-local variables. In order for the approach to work, the client-provided string keys have to be unique: if two clients independently decide to use the same name for their thread-local variable, they unintentionally share a single variable, which will generally cause both clients to fail. Also, the security is poor. A malicious client could intentionally use the same string key as another client to gain illicit access to the other client’s data. 這種方法的問題在于,字符串鍵表示線程本地變量的共享全局名稱空間。為了使這種方法有效,客戶端提供的字符串鍵必須是惟一的:如果兩個客戶端各自決定為它們的線程本地變量使用相同的名稱,它們無意中就會共享一個變量,這通常會導致兩個客戶端都失敗。而且,安全性很差。惡意客戶端可以故意使用與另一個客戶端相同的字符串密鑰來非法訪問另一個客戶端的數據。 This API can be fixed by replacing the string with an unforgeable key (sometimes called a capability): 這個 API 可以通過用一個不可偽造的鍵(有時稱為 capability)替換字符串來修復: ``` public class ThreadLocal { private ThreadLocal() { } // Noninstantiable public static class Key { // (Capability) Key() { } } // Generates a unique, unforgeable key public static Key getKey() { return new Key(); } public static void set(Key key, Object value); public static Object get(Key key); } ``` While this solves both of the problems with the string-based API, you can do much better. You don’t really need the static methods anymore. They can instead become instance methods on the key, at which point the key is no longer a key for a thread-local variable: it is a thread-local variable. At this point, the toplevel class isn’t doing anything for you anymore, so you might as well get rid of it and rename the nested class to ThreadLocal: 雖然這解決了 API 中基于字符串的兩個問題,但是你可以做得更好。你不再真正需要靜態方法。它們可以變成鍵上的實例方法,此時鍵不再是線程局部變量的鍵值:而是成為線程局部變量本身。此時,頂層類不再為你做任何事情,所以你可以刪除它,并將嵌套類重命名為 ThreadLocal: ``` public final class ThreadLocal { public ThreadLocal(); public void set(Object value); public Object get(); } ``` This API isn’t typesafe, because you have to cast the value from Object to its actual type when you retrieve it from a thread-local variable. It is impossible to make the original String-based API typesafe and difficult to make the Keybased API typesafe, but it is a simple matter to make this API typesafe by making ThreadLocal a parameterized class (Item 29): 這個 API 不是類型安全的,因為在從線程本地變量檢索值時,必須將值從 Object 轉換為它的實際類型。原始的基于 String 類型 API 的類型安全是不可能實現的,基于鍵的 API 的類型安全也是很難實現的,但是通過將 ThreadLocal 作為一個參數化的類來實現這個 API 的類型安全很簡單([Item-29](/Chapter-5/Chapter-5-Item-29-Favor-generic-types.md)): ``` public final class ThreadLocal<T> { public ThreadLocal(); public void set(T value); public T get(); } ``` This is, roughly speaking, the API that java.lang.ThreadLocal provides. In addition to solving the problems with the string-based API, it is faster and more elegant than either of the key-based APIs. 粗略地說,這就是 `java.lang.ThreadLocal` 提供的 API,除了解決基于字符串的問題之外,它比任何基于鍵的 API 都更快、更優雅。 To summarize, avoid the natural tendency to represent objects as strings when better data types exist or can be written. Used inappropriately, strings are more cumbersome, less flexible, slower, and more error-prone than other types. Types for which strings are commonly misused include primitive types, enums, and aggregate types. 總之,當存在或可以編寫更好的數據類型時,應避免將字符串用來表示對象。如果使用不當,字符串比其他類型更麻煩、靈活性更差、速度更慢、更容易出錯。字符串經常被誤用的類型包括基本類型、枚舉和聚合類型。 --- **[Back to contents of the chapter(返回章節目錄)](/Chapter-9/Chapter-9-Introduction.md)** - **Previous Item(上一條目):[Item 61: Prefer primitive types to boxed primitives(基本數據類型優于包裝類)](/Chapter-9/Chapter-9-Item-61-Prefer-primitive-types-to-boxed-primitives.md)** - **Next Item(下一條目):[Item 63: Beware the performance of string concatenation(當心字符串連接引起的性能問題)](/Chapter-9/Chapter-9-Item-63-Beware-the-performance-of-string-concatenation.md)**
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看