|
July 23, 1998 This issue presents tips, techniques, and sample code for the following topics:
Stream Tokenizing There's also another way to do tokenization, using java.io.StreamTokenizer. StreamTokenizer operates on input streams rather than strings, and each byte in the input stream is regarded as a character in the range '\u0000' through '\u00FF'. StreamTokenizer is lower level than StringTokenizer, but offers more control over the tokenization process. The class uses an internal table to control how tokens are parsed, and this syntax table can be modified to change the parsing rules. Here's an example of how StreamTokenizer works:
In this example, a StreamTokenizer is created on top of a FileReader / BufferedReader pair that represents a text file. Note that a StreamTokenizer can also be made to read from a String by using StringReader as illustrated in the commented-out code shown above (StringBufferInputStream also works, although this class has been deprecated). The method resetSyntax is used to clear the internal syntax table, so that StreamTokenizer forgets any rules that it knows about parsing tokens. Then wordChars is used to declare that only upper and lower case letters should be considered to form words. That is, the only tokens that StreamTokenizer recognizes are sequences of upper and lower case letters. nextToken is called repeatedly to retrieve words, and each resulting word is found in the public instance variable "st.sval". The words are inserted into a Hashtable, and at the end of processing the contents of the table are displayed, using an Enumeration as illustrated in Tech Tips: June 23, 1998. So the action of this program is to find all the unique words in a text file and display them. StreamTokenizer also has special facilities for parsing numbers, quoted strings, and comments. It's a useful alternative to StringTokenizer, and is especially applicable if you are tokenizing input streams, or wish to exercise finer control over the tokenization process.
Division By Zero 1.0 / 0.0 What happens? Does this usage cause an exception to be thrown? Is the result undefined? In the Java programming language, integral division by zero results in an ArithmeticException. But for floating-point, no exception is thrown (in C++ the result of division by zero is undefined). The result of 1.0 / 0.0 is positive infinity, indicated by the constant Double.POSITIVE_INFINITY (which, in fact, is defined by performing this division). This example illustrates an important point, which is that Java language floating-point arithmetic operates according to a well-defined standard, known as IEEE 754. Another related idea is that of NaN (not a number), used to represent the results of certain arithmetic operations such as the following: 0.0 / 0.0 There is also a "Double.NaN" constant defined, which is analogous to the constant Double.POSITIVE_INFINITY. NaN is interesting in that it has the following property: NaN != NaN In other words, NaN is unequal to itself, and this fact is used to implement methods such as Double.isNaN. To tie down these ideas a little better, here is an example that uses negative infinity, positive infinity, and NaN:
The output of the program is the following: fff0000000000000 7ff0000000000000 7ff8000000000000 These numbers are the hexadecimal 64-bit values that represent negative infinity, positive infinity, and NaN respectively. In other words, particular bit patterns for a double value indicate specific special values such as NaN. Finally, knowing how floating-point arithmetic behaves is quite important in particular applications, and the Java language specification goes to some lengths to tie down behavior in this area. | |||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||