Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
StringBuffer object.
Character is an example.
These tips and cautions will help you write better programs and save you from agonizing over why the compiler produces error messages.
intern() to intern your Strings in the common string memory pool. Because that pool contains no duplicate Strings, each object has a unique reference. Plus, using == to compare references proves faster than using a method to compare a string's characters.
StringBuffer to create a new character array and copy characters from the old array to the new array (during an expansion), use ensureCapacity(int minimumCapacity) to minimize expansions prior to entering a loop that appends many characters to a StringBuffer. That improves performance.
concat(String str) or the string concatenation operator in a loop that executes repeatedly; that can affect your program's performance. (Each
approach creates several objects—which can increase garbage collections—and several methods are called behind the scenes.)
length() with length leads to compiler errors. length() is a method that returns the current number of characters in a String's value array, whereas length is a read-only array field that returns the maximum number of elements in an array.
HexDec includes the expression i < s.length () in its for loop header. For long loops, do not call length() in a for loop header because of method call overhead (which can affect performance). Instead, call that method and save its return
value before entering the loop, and then use the saved value in the loop header. Example: int len = s.length (); for (int i = 0; i < len; i++). For toy programs, like HexDec, it doesn't matter if I call length() in the for loop header. But for professional programs where performance matters, every time-saving trick helps. (Some Java compilers
perform this optimization for you.)
countTokens()'s return value to control a string tokenization loop's duration if the loop changes the set of delimiters via a nextToken(String delim) method call. Failure to heed that advice often leads to one of the nextToken() methods throwing a NoSuchElementException object and the program terminating prematurely.
Subsequent to the publication of my article on character and string classes, a problem with the StringTokenizer class was brought to my attention. The problem deals with the String delim parameter that is part of two StringTokenizer constructors. To many developers, that parameter's String type suggests that StringTokenizer recognizes multicharacter delimiters (such as ###). Instead, StringTokenizer interprets that parameter as a set of one-character delimiters. This confusion over what delim means would disappear if delim had character array type (char []). For more information on the problem and a multicharacter delimiter solution, read "Steer Clear of Java Pitfalls," Michael Daconta (JavaWorld, September 2000).
Find out what questions your fellow readers are asking and my answers to those questions.
Jeff,
When you need to create strings dynamically, what should you use performance wise, String or StringBuffer? I thought StringBuffer performs better than String. So I always start with a StringBuffer, and after completing the String, return a buffer.toString().
Michel
Character class?
String objects?
Editor application with the following capabilities:DELFCH to DELCH and modify that command to take a single integer argument identifying the zero-based index of the character to delete in
the current line. Example: delch 2 deletes the current line's third character. Provide appropriate error checking to warn users when they specify an invalid
index (or curline contains -1, indicating no lines of text). If no more characters are in the current line, delete it and update curline as appropriate.
DEL command that deletes the current line. Use error checking to deal with the situation when curline contains -1. Update curline as appropriate.
REPL command that replaces all occurrences of a specific character in the current line with another character. Two character arguments
should follow REPL: the first argument identifies the character to replace, and the second argument identifies its replacement character. Example:
repl # * replaces all occurrences of # with *. Use appropriate error checking in case no current line exists (i.e., curline contains -1).
SETCL command that takes a single zero-based integer argument and sets curline to that value. Use appropriate error checking in case the value is out of range or curline contains -1.
LOAD and SAVE commands that let you load the contents of arbitrary text files and save the current text to a specific text file. What sort
of error checking will you need?
Last month, I asked you answer some questions and create a package. My answers appear in red.