Thomas Duff, Portland Domino/Notes User Group, July 2002
William Wagers, Focus on Java, July 2002
Thomas Duff, Portland Domino/Notes User Group, July 2002
William Wagers, Focus on Java, July 2002
Product Description
Java just keeps growing, adding features, functionality, complexity, and tempting developers to growl with frustration. The new 1.4 release of Java 2 Standard edition increases the size of the platform by 50%, to 2757 classes in 135 packages. How are you going to figure out what this means for your applications? As always, Java in a Nutshell has the answers. The new 4th edition still contains an accelerated introduction to the Java programming language and its key APIs so you can start writing code right away. And with more than 250 new pages, author David Flanagan quickly brings you up to speed on new features that come with version 1.4:
- High-performance NIO API
- Support for pattern matching with regular expressions
- A logging API
- A user preferences API
- New Collections classes
- An XML-based persistence mechanism for Java Beans
- Support for XML parsing using both the DOM and SAX APIs
- User authentication with the JAAS API
- Support for secure network connections using the SSL protocol
- Support for cryptography
The book contains O'Reilly's classic quick-reference for all the classes in the essential Java packages, so you can dive in and find what you need to make the new 1.4 version work for you. For as long as Java developers have existed, Java in a Nutshell has been ready, willing and able to take you right to the heart of the program, turning those frustrated grrrrss into purrrss of satisfaction. No wonder readers of Java Developer's Journal voted this the "Best Java Book" the past two years in a row!
From the Publisher
About the Author
David Flanagan is a computer programmer who spends most of his time writing about JavaScript and Java. His books with O'Reilly include Java in a Nutshell, Java Examples in a Nutshell, Java Foundation Classes in a Nutshell, JavaScript: The Definitive Guide, and JavaScript Pocket Reference. David has a degree in computer science and engineering from the Massachusetts Institute of Technology. He lives with his wife and son in the U.S. Pacific Northwest bewteen the cities of Seattle, Washington and Vancouver, British Columbia. David has a simple website at http://www.davidflanagan.com.
Excerpted from Java in a Nutshell by David Flanagan. Copyright © 2002. Reprinted by permission. All rights reserved.
Chapters 2 and 3 documented the Java programming language. This chapter switches gears and covers the Java platform -- a vast collection of predefined classes available to every Java program, regardless of the underlying host system on which it is running. The classes of the Java platform are collected into related groups, known as packages. This chapter begins with an overview of the packages of the Java platform that are documented in this book. It then moves on to demonstrate, in the form of short examples, the most useful classes in these packages. Most of the examples are code snippets only, not full programs you can compile and run. For fully fleshed-out, real-world examples, see Java Examples in a Nutshell (O'Reilly). That book expands greatly on this chapter and is intended as a companion to this one.
Java Platform Overview
Table 1-1 does not list all the packages in the Java platform, only those documented in this book. (And it omits a few "spi" packages that are documented in this book but are of interest only to low-level "service providers.") Java also defines numerous packages for graphics and graphical user interface programming and for distributed, or enterprise, computing. The graphics and GUI packages are java.awt and javax.swing and their many subpackages. These packages, along with the java.applet package, are documented in Java Foundation Classes in a Nutshell (O'Reilly). The enterprise packages of Java include java.rmi, java.sql, javax.jndi, org.omg.CORBA, org.omg.CosNaming, and all of their subpackages. These packages, as well as several standard extensions to the Java platform, are documented in Java Enterprise in a Nutshell (O'Reilly).
Strings and Characters
Strings of text are a fundamental and commonly used data type. In Java, however, strings are not a primitive type, like char, int, and float. Instead, strings are represented by the java.lang.String class, which defines many useful methods for manipulating strings. String objects are immutable: once a String object has been created, there is no way to modify the string of text it represents. Thus, each method that operates on a string typically returns a new String object that holds the modified string.
This code shows some of the basic operations you can perform on strings:
// Creating strings
String s = "Now"; // String objects have a special literal syntax
String t = s + " is the time."; // Concatenate strings with + operator
String t1 = s + " " + 23.4; // + converts other values to strings
t1 = String.valueOf('c'); // Get string corresponding to char value
t1 = String.valueOf(42); // Get string version of integer or any value
t1 = object.toString(); // Convert objects to strings with toString()
// String length
int len = t.length(); // Number of characters in the string: 16
// Substrings of a string
String sub = t.substring(4); // Returns char 4 to end: "is the time."
sub = t.substring(4, 6); // Returns chars 4 and 5: "is"
sub = t.substring(0, 3); // Returns chars 0 through 2: "Now"
sub = t.substring(x, y); // Returns chars between pos x and y-1
int numchars = sub.length(); // Length of substring is always (y-x)
// Extracting characters from a string
char c = t.charAt(2); // Get the 3rd character of t: w
char[] ca = t.toCharArray(); // Convert string to an array of characters
t.getChars(0, 3, ca, 1); // Put 1st 3 chars of t into ca[1]-ca[3]
// Case conversion
String caps = t.toUpperCase(); // Convert to uppercase
String lower = t.toLowerCase(); // Convert to lowercase
// Comparing strings
boolean b1 = t.equals("hello"); // Returns false: strings not equal
boolean b2 = t.equalsIgnoreCase(caps); // Case-insensitive compare: true
boolean b3 = t.startsWith("Now"); // Returns true
boolean b4 = t.endsWith("time."); // Returns true
int r1 = s.compareTo("Pow"); // Returns 0: s comes after "Mow"
r1 = s.compareToIgnoreCase("pow"); // Returns < 0 (Java 1.2 and later)
// Searching for characters and substrings
int pos = t.indexOf('i'); // Position of first 'i': 4
pos = t.indexOf('i', pos+1); // Position of the next 'i': 12
pos = t.indexOf('i', pos+1); // No more 'i's in string, returns -1
pos = t.lastIndexOf('i'); // Position of last 'i' in string: 12
pos = t.lastIndexOf('i', pos-1); // Search backwards for 'i' from char 11
pos = t.indexOf("is"); // Search for substring: returns 4
pos = t.indexOf("is", pos+1); // Only appears once: returns -1
pos = t.lastIndexOf("the "); // Search backwards for a string
String noun = t.substring(pos+4); // Extract word following "the"
// Replace all instances of one character with another character
String exclaim = t.replace('.', '!'); // Works only with chars, not substrings
// Strip blank space off the beginning and end of a string
String noextraspaces = t.trim();
// Obtain unique instances of strings with intern()
String s1 = s.intern(); // Returns s1 equal to s
String s2 = "Now".intern(); // Returns s2 equal to "Now"
boolean equals = (s1 == s2); // Now can test for equality with ==