Monday, 9 June 2008

Core Java™ Volume II By Cay S. Horstmann and Gary Cornell

Just now i completed reading and practicing "Core Java™ Volume II–Advanced Features, Eighth Edition" By Cay S. Horstmann; Gary Cornell.

This book is Winner of the 2003 Jolt/Software Development Productivity Award!

Please read reviews and interviews of the author of this great book @book site

I safely skipped chapter 12th Native Programming and 11th about Scripting,Compiling and Annotation Processing chapter 6th about Swings and Chapter 7 about Java 2D API and even 8th Chapter about almost what i thought obselete Java Beans Programming.

Below are the points i wanted to share with readers from this book.

Chapter 1: Streams and Files

1) Because byte-oriented streams are inconvenient for processing information stored in Unicode (recall that Unicode uses multiple bytes per character), there is a separate hierarchy of classes for processing Unicode characters that inherit from the abstract Reader and Writer classes. These classes have read and write operations that are based on two-byte Unicode code units rather than on single-byte characters.

2) Both the read and write methods block until the bytes are actually read or written. This means that if the stream cannot immediately be accessed (usually because of a busy network connection), the current thread blocks. This gives other threads the chance to do useful work while the method is waiting for the stream to again become available.

3) In particular, if you do not close a file, the last packet of bytes might never be delivered. You can also manually flush the output with the flush method.

4) In Java 1.0, the PrintStream class simply truncated all Unicode characters to ASCII characters by dropping the top byte. Clearly, that was not a clean or portable approach, and it was fixed with the introduction of readers and writers in Java 1.1.For compatibility with existing code, System.in, System.out, and System.err are still streams, not readers and writers.

5) To write data in binary format, you use a DataOutputStream.To write in text format, you use a PrintWriter.

6) To find out which character sets are available in a particular implementation, call the static availableCharsets method. Use this code to find out the names of all available character sets:


Map<String, Charset> charsets = Charset.availableCharsets();
for (String name : charsets.keySet())
System.out.println(name);


7) There are two different methods of storing integers and floating-point numbers in memory, depending on the platform you are using. Suppose, for example, you are working with a 4-byte int, say the decimal number 1234, or 4D2 in hexadecimal (1234 = 4 x 256 + 13 x 16 + 2). This can be stored in such a way that the first of the 4 bytes in memory holds the most significant byte (MSB) of the value: 00 00 04 D2. This is the so-called big-endian method. Or we can start with the least significant byte (LSB) first: D2 04 00 00. This is called, naturally enough, the little-endian method. For example, the SPARC uses big-endian; the Pentium, little-endian. This can lead to problems. When a C or C++ file is saved, the data are saved exactly as the processor stores them. That makes it challenging to move even the simplest data files from one platform to another. In Java, all values are written in the big-endian fashion, regardless of the processor. That makes Java data files platform independent.

8)

a) The object stream output contains the types and data fields of all objects.

b) Each object is assigned a serial number.

c) Repeated occurrences of the same object are stored as references to that serial number

9) Serialization is somewhat slow because the virtual machine must discover the structure of each object. If you are concerned about performance and if you read and write a large number of objects of a particular class, you should investigate the use of the Externalizable interface. The tech tip http://java.sun.com/developer/TechTips/2000/tt0425.html demonstrates that in the case of an employee class, using external reading and writing was about 35 to 40 percent faster than the default serialization.

Chapter 3:Networking

1) To interrupt a socket operation, you use a SocketChannel, a feature of the java.nio package. Open the SocketChannel like this:

SocketChannel channel = SocketChannel.open(new InetSocketAddress(host, port));

2) As of Java SE 1.4, Secure Sockets Layer (SSL) support is a part of the standard library. Before Java SE 1.4, you were only able to make SSL connections from applets by taking advantage of the SSL implementation of the browser.

Chapter 4: Database Programming

1) the ultimate goal of JDBC is to make possible the following:

a) Programmers can write applications in the Java programming language to access any database, using standard SQL statements—or even specialized extensions of SQL—while still following Java language conventions.

b) Database vendors and database tool vendors can supply the low-level drivers. Thus, they can optimize their drivers for their specific products.

2) If you are curious as to why Sun just didn't adopt the ODBC model, their response, as given at the JavaOne conference in May 1996, was this:

ODBC is hard to learn.

ODBC has a few commands with lots of complex options. The preferred style in the Java programming language is to have simple and intuitive methods, but to have lots of them.

ODBC relies on the use of void* pointers and other C features that are not natural in the Java programming language.

An ODBC-based solution is inherently less safe and harder to deploy than a pure Java solution

3) LDAP is most commonly used for the storage of directories that contain data such as user names, passwords, and permissions.

Chapter 5 Internationalization

1) Objects of type Number are not automatically unboxed—you cannot simply assign a Number object to a primitive type. Instead, use the doubleValue or intValue method.

2) compareTo method in the Java programming language uses the values of the Unicode character to determine the ordering.For example, lowercase characters have a higher Unicode value than do uppercase characters, and accented characters have even higher values. This leads to absurd results; for example, the following five strings are ordered according to the compareTo method:

America
Zulu
able
zebra
Ångström

Fortunately, once you are aware of the problem, collation is quite easy. As always, you start by obtaining a Locale object. Then, you call the getInstance factory method to obtain a Collator object. Finally, you use the compare method of the collator, not the compareTo method of the String class, whenever you want to sort strings.


Locale loc = . . .;
Collator coll = Collator.getInstance(loc);
if (coll.compare(a, b) < 0) // a comes before b . . .;



Most important, the Collator class implements the Comparator interface. Therefore, you can pass a collator object to the Collections.sort method to sort a list of strings:

Collections.sort(strings, coll);

Chapter 9 Security

1) A Java compiler converts source instructions for the Java virtual machine. The virtual machine code is stored in a class file with a .class extension. Each class file contains the definition and implementation code for one class or interface. These class files must be interpreted by a program that can translate the instruction set of the virtual machine into the machine language of the target machine.

2) Note that the virtual machine loads only those class files that are needed for the execution of a program. For example, suppose program execution starts with MyProgram.class. Here are the steps that the virtual machine carries out.

a. The virtual machine has a mechanism for loading class files, for example, by reading the files from disk or by requesting them from the Web; it uses this mechanism to load the contents of the MyProgram class file.

b. If the MyProgram class has fields or superclasses of another class type, their class files are loaded as well. (The process of loading all the classes that a given class depends on is called resolving the class.)

c. The virtual machine then executes the main method in MyProgram (which is static, so no instance of a class needs to be created).

d. If the main method or a method that main calls requires additional classes, these are loaded next

3) The class loading mechanism doesn't just use a single class loader, however. Every Java program has at least three class loaders:

The bootstrap class loader

The extension class loader

The system class loader (also sometimes called the application class loader)

4) The bootstrap class loader loads the system classes (typically, from the JAR file rt.jar). It is an integral part of the virtual machine and is usually implemented in C. There is no ClassLoader object corresponding to the bootstrap class loader. For example,

String.class.getClassLoader()

returns null.

5) The extension class loader loads "standard extensions" from the jre/lib/ext directory. You can drop JAR files into that directory, and the extension class loader will find the classes in them, even without any class path. (Some people recommend this mechanism to avoid the "class path from hell," but see the next cautionary note.)

The system class loader loads the application classes. It locates classes in the directories and JAR/ZIP files on the class path, as set by the CLASSPATH environment variable or the -classpath command-line option.

In Sun's Java implementation, the extension and system class loaders are implemented in Java. Both are instances of the URLClassLoader class

6) Please note that we can run into grief if you drop a JAR file into the jre/lib/ext directory and one of its classes needs to load a class that is not a system or extension class. The extension class loader does not use the class path. Keep that in mind before you use the extension directory as a way to manage your class file hassles.

7) Class loaders have a parent/child relationship. Every class loader except for the bootstrap class loader has a parent class loader. A class loader is supposed to give its parent a chance to load any given class and only load it if the parent has failed. For example, when the system class loader is asked to load a system class (say, java.util.ArrayList), then it first asks the extension class loader. That class loader first asks the bootstrap class loader. The bootstrap class loader finds and loads the class in rt.jar, and neither of the other class loaders searches any further.

8) Every Java programmer knows that package names are used to eliminate name conflicts. There are two classes called Date in the standard library, but of course their real names are java.util.Date and java.sql.Date. The simple name is only a programmer convenience and requires the inclusion of appropriate import statements. In a running program, all class names contain their package name.

It might surprise you, however, that you can have two classes in the same virtual machine that have the same class and package name. A class is determined by its full name and the class loader. This technique is useful for loading code from multiple sources. For example, a browser uses separate instances of the applet class loader class for each web page. This allows the virtual machine to separate classes from different web pages, no matter what they are named. Figure 9-2 shows an example. Suppose a web page contains two applets, provided by different advertisers, and each applet has a class called Banner. Because each applet is loaded by a separate class loader, these classes are entirely distinct and do not conflict with each other

9) When a class loader presents the bytecodes of a newly loaded Java platform class to the virtual machine, these bytecodes are first inspected by a verifier. The verifier checks that the instructions cannot perform actions that are obviously damaging. All classes except for system classes are verified. You can, however, deactivate verification with the undocumented -noverify option.

10) Here are some of the checks that the verifier carries out:

Variables are initialized before they are used.

Method calls match the types of object references.

Rules for accessing private data and methods are not violated.

Local variable accesses fall within the runtime stack.

The runtime stack does not overflow.

If any of these checks fails, then the class is considered corrupted and will not be loaded

11) Once a class has been loaded into the virtual machine and checked by the verifier, the second security mechanism of the Java platform springs into action: the security manager. The security manager is a class that controls whether a specific operation is permitted. Operations checked by the security manager include the following:

Creating a new class loader

Exiting the virtual machine

Accessing a field of another class by using reflection

Accessing a file

Opening a socket connection

Starting a print job

Accessing the system clipboard

Accessing the AWT event queue

Bringing up a top-level window

Chapter 10th : Distributed Programming

1) What we want is a mechanism by which the client programmer makes a regular method call, without worrying about sending data across the network or parsing the response. The solution is to install a proxy object on the client. The proxy is an object located in the client virtual machine that appears to the client program as if it was the remote object. The client calls the proxy, making a regular method call. The client proxy contacts the server, using a network protocol.Unlike RMI, CORBA and SOAP are completely language neutral. Client and server programs can be written in C, C++, C#, Java, or any other language.

2) Author's sentiments about CORBA are similar to those expressed by French president Charles De Gaulle about Brazil: It has a great future . . . and always will.

3) Remote objects are garbage-collected automatically, just as local objects are. However, the distributed collector is signifcantly more complex. When the local garbage collector finds that there are further local uses of a remote reference, it notifies the distributed collector that the server is no longer referenced by this client. When a server is no longer used by any clients, it is marked as garbage.

4) Whenever a program loads new code from another network location, there is a security issue. For that reason, you need to use a security manager in RMI applications that dynamically load classes.

5) There are two mechanisms for transferring values between virtual machines.

a) Objects of classes that implement the Remote interface are transferred as remote references.

b) Objects of classes that implement the Serializable interface but not the Remote interface are copied using serialization

About the Authors
Cay S. Horstmann has written many books on C++, Java and object-oriented development, is the series editor for Core Books at Prentice-Hall and a frequent speaker at computer industry conferences. For four years, Cay was VP and CTO of an Internet startup that went from 3 people in a tiny office to a public company. He is now a computer science professor at San Jose State University .

Gary Cornell has authored or co-authored 14 popular computer books and articles for many developer magazines. Gary is a professor at the University of Connecticut and one of the founders of the Apress publishing company.

Hope you enjoy reading this book.

No comments: