Monday, January 11, 2010

10. Strings











 < Day Day Up > 







10. Strings



This section concern character strings.





10.1. Arrays do not override Object.toString



Prescription: For char arrays, use String.valueOf to obtain the string representing the designated sequence of characters. For other types of arrays, use Arrays.toString or, prior to release 5.0, Arrays.asList.



References: Puzzle 12; [JLS 10.7].







10.2. String.replaceAll takes a regular expression as its first argument



Prescription: Ensure that the argument is a legal regular expression, or use String.replace instead.



References: Puzzle 20.







10.3. String.replaceAll takes a replacement string as its second argument



Prescription: Ensure that the argument is a legal replacement string, or use String.replace instead.



References: Puzzle 20.







10.4. Repeated string concatenation can cause poor performance



Prescription: Avoid using the string concatenation operator in loops.



References: [EJ item 33].







10.5. Conversion of bytes to characters requires a charset



Prescription: Always select a charset when converting a byte array to a string or char array; if you don't, the platform default charset will be used, leading to unpredictable behavior.



References: Puzzle 18.







10.6. Values of type char are silently converted to int, not String



Prescription: To convert a char to a string, use String.valueOf(char).



References: Puzzles 11 and 23; [JLS 5.1.2].

















     < Day Day Up > 



    No comments: