Monday, November 24, 2008

Advantage and disadvantages of String Implementation in JAVA

Advantages of the String implementation in JAVA 1. Compilation creates unique strings. At compile time, strings are resolved as far as possible. This includes applying the concatenation operator and converting other literals to strings. So hi7 and (hi+7) both get resolved at compile period to the same string, and are identical objects in the class string pool. Compilers differ in their ability to achieve this resolution. You can always check your compiler (e.g., by decompiling some statements involving concatenation) and alter it if needed. 2. Since String objects are immutable, a substring operation doesn"t demand to copy the entire underlying sequence of characters. Instead, a substring can application the same char array as the original string and simply refer to a different open mark and endpoint in the char array. This method that substring operations are efficient, being both speedy and conserving of memory; the extra object is just a wrapper on the same underlyi
ng char array with different pointers into that array. 3. Strings are implemented in the JDK as an internal char array with index offsets (actually a launch offset and a character count). This basic structure is extremely unlikely to be changed in any version of Java. 4. Strings have strong support for internationalization. It would accept a large effort to reproduce the internationalization support for an alternative class. 5. The close relationship with StringBuffers allows Strings to reference the same char array used by the StringBuffer. This is a double-edged sword. For typical practice, when you handle a StringBuffer to manipulate and append characters and data types, and then convert the final result to a String, this works just fine. The StringBuffer provides efficient mechanisms for growing, inserting, appending, altering, and other types of String manipulation. The resulting String then efficiently references the same char array with no extra character copyi
ng. This is very hurried and reduces the number of objects being used to a minimum by avoiding intermediate objects. However, if the StringBuffer object is subsequently altered, the char array in that StringBuffer is copied into a fresh char array that is nowadays referenced by the StringBuffer. The String object retains the reference to the previously shared char array. This way that copying overhead can occur at unexpected points in the application. Instead of the copying occurring at the toString( ) method call, as might be expected, any subsequent alteration of the StringBuffer causes a virgin char array to be created and an array copy to be performed. To cause the copying overhead occur at predictable times, you could explicitly execute some method that makes the copying occur, such as StringBuffer.setLength( ). This allows StringBuffers to be reused with more predictable performance. The disadvantages of the String implementation are 1. Not being able to subclass
String income that it is not credible to add behavior to String for your own needs. Full text: http://computerandtechnologies.com/computers-and-technology/news_2008-11-24-23-30-03-943.html

No comments: