Italic is used for:
Filenames (
readme.txt
)Host and domain names (http://www.oreilly.com)
Constant width
is used for:
Code examples and fragments
Class, variable, and method names, and Java keywords used within the text
Significant code fragments and complete programs are generally placed in a separate paragraph like this:
InputStream in = new FileInputStream("/etc/mailcap");
When code is presented as fragments rather than complete programs,
the existence of the appropriate import
statements
should be inferred. For example, in the previous code fragment you
may assume that java.io.InputStream
and
java.io.FileInputStream
were imported.
Some examples intermix user input with program output. In these cases, the user input will be displayed in bold, but otherwise in the same monospaced font, as in this example from Chapter 17:
D:\JAVA\16>java PortTyper COM2
at&f
at&f OKatdt 321-1444
Most of the code examples in this book are optimized for legibility
rather than speed. For instance, consider this
getIcon()
method from Chapter 13:
public Icon getIcon(File f) { if (f.getName().endsWith(".zip")) return zipIcon; if (f.getName().endsWith(".gz")) return gzipIcon; if (f.getName().endsWith(".dfl")) return deflateIcon; return null; }
I invoke the f.getName()
method three times, when
once would do:
public Icon getIcon(File f) { String name = f.getName(); if (name.endsWith(".zip")) return zipIcon; if (name.endsWith(".gz")) return gzipIcon; if (name.endsWith(".dfl")) return deflateIcon; return null; }
However, this seemed slightly less obvious than the first example. Therefore, I chose the marginally slower form. Other, still less obvious optimizations are also possible, but would only make the code even more obscure. For example:
public Icon getIcon(File f) { String name = f.getName(); String lastDot = name.lastIndexOf('.'); if (lastDot != -1) { String extension = name.substring(lastDot+1); if (extension.equals("zip")) return zipIcon; if (extension.equals("gz")) return gzipIcon; if (extension.equals("dfl")) return deflateIcon; } return null; }
I might resort to this form if profiling proved that this method was a performance bottleneck in my application, and this revised method was genuinely faster, but I certainly wouldn’t use it in my first pass at the problem. In general, I only optimize for speed when similar code seems likely to be a performance bottleneck in situations where it’s likely to be used, or when optimizing can be done without negatively affecting the legibility of the code.
Finally, although many of the examples are toys unlikely to be
reused, a few of the classes I develop have real value. Please feel
free to reuse them or any parts of them in your own code; no special
permission is required. Such classes are placed somewhere in the
com.macfaq
package, generally mirroring the
java
package hierarchy. For instance, Chapter 2’s NullOutputStream
class is in the com.macfaq.io
package; its
StreamedTextArea
class is in the
com.macfaq.awt
package. When working with these
classes, don’t forget that the compiled
.class
files must reside in directories matching
their package structure inside your class path and that you’ll
have to import them in your own classes before you can use
them.[2] The web page includes a JAR
file that can be installed in your class path.
Furthermore, classes not in the default package with
main()
methods are generally run by passing in the
full package-qualified name. For example:
D:\JAVA\ioexamples\04>java com.macfaq.io.FileCopier oldfile newfile
[2] See “The Name Space: Packages, Classes, and Members” in the second edition of David Flanagan’s Java in a Nutshell (O’Reilly & Associates, 1997).
Get Java I/O now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.