Thursday, 18 September 2008

Java 2 Certification By Phillip Heller, Simon Roberts - Part2

Couple Of Days back i started reading Complete Java 2 Certification Study Guide, 5th Edition By Phillip Heller, Simon Roberts.

I wanted to share few important quotations i found from the next 2 chapters(3-4) of the book.

1) Modifier Overview
Modifiers are Java keywords that give the compiler information about the nature of code, data,
or classes. Modifiers specify, for example, that a particular feature is static, final, or transient.
(A feature is a class, a method, or a variable.) A group of modifiers, called access modifiers , dictates
which classes are allowed to use a feature. Other modifiers can be used in combination to describe the attributes of a feature.

The most common modifiers are the access modifiers:

public
private
protected

The remaining modifiers do not fall into any clear categorization. They are

final
abstract
static
native
transient
synchronized
volatile

2) The rules for overriding can be summarized as follows:

A private method may be overridden by a private, default, protected, or public method.
A default method may be overridden by a default, protected, or public method.
A protected method may be overridden by a protected or public method.
A public method may be overridden only by a public method.

3) To summarize static methods:
A static method may access only the static data of its class; it may not access nonstatic data.
A static method may call only the static methods of its class; it may not call nonstatic methods.
A static method has no this.
A static method may not be overridden to be nonstatic.

4) Static Initializers

It is legal for a class to contain static code that does not exist within a method body. A class may have a block of initializer code that is simply surrounded by curly braces and labeled static.

For example:

1. public class StaticExample {
2. static double d = 1.23;
3.
4. static {
5. System.out.println("Static code: d=" + d++);
6. }
7.
8. public static void main(String args[]) {
9. System.out.println("main: d = " + d++);
10. }
11. }

Something seems to be missing from line 4. You might expect to see a complete method declaration there: static void printAndBump(), for example, instead of just static. In fact, line 4 is perfectly valid; it is known as static initializer code. The code inside the curlies is executed exactly once, at the time the class is loaded. At class-load time, all static initialization (such as line 2) and all free-floating static code (such as lines 4–6) are executed in order of appearance within the class definition.

The output from this code is:
Static code: d=1.23
main: d = 2.23

5) native

The native modifier can refer only to methods. Like the abstract modifier, native indicates that the body of a method is to be found elsewhere. In the case of abstract methods, the body is in a subclass; with native methods, the body lies entirely outside the Java Virtual Machine (JVM), in a library.

Native code is written in a non-Java language, typically C or C++, and compiled for a single target machine type. (Thus Java’s platform independence is violated.) People who port Java to new platforms implement extensive native code to support GUI components, network communication, and a broad range of other platform-specific functionality. However, it is rare for application and applet programmers to need to write native code.

One technique, however, is of interest in light of the last section’s discussion of static code. When a native method is invoked, the library that contains the native code ought to be loaded and available to the JVM; if it is not loaded, there will be a delay. The library is loaded by calling System.loadLibrary (“library_name”) and, to avoid a delay, it is desirable to make this call as early as possible. Often programmers will use the technique shown in the following code sample, which assumes the library name is MyNativeLib:

a. class NativeExample {
b. native void doSomethingLocal(int i);
c.
d. static {
e. System.loadLibrary("MyNativeLib");
f. }
g. }

6) volatile
The volatile modifier is not in common use. Only variables may be volatile; declaring them so indicates that such variables might be modified asynchronously, so the compiler takes special precautions. Volatile variables are of interest in multiprocessor environments.

7) All Possible Combinations of Features and Modifiers














ModifierClassVariableMethodConstructorFree-Floating Block
publicyesyesyesyesno
protectednoyesyesyesno
defaultyesyesyesyesyes
privatenoyesyesyesno
finalnoyesyesnono
abstractyesnoyesnono
staticnoyesyesnoyes
nativenonoyesnono
transientnoyesnonono
volatilenoyesnonono
synchronizednonoyesnoyes


8) The general rules for primitive assignment conversion can be stated as follows:
a) A boolean cannot be converted to any other type.
b) A nonboolean can be converted to another nonboolean type, provided the conversion is a
widening conversion.
c) A nonboolean cannot be converted to another nonboolean type if the conversion would be a
narrowing conversion.

9) Java’s widening conversions are
From a byte to a short , an int , a long, a float, or a double
From a short to an int, a long, a float, or a double
From a char to an int, a long, a float, or a double
From an int to a long, a float, or a double
From a long to a float or a double
From a float to a double

10) Primitive Conversion: Method Call

Another kind of conversion is method-call conversion. A method-call conversion happens when you pass a value of one type as an argument to a method that expects a different type. For example, the cos() method of the Math class expects a single argument of type double. Consider the following code:

1. float frads;
2. double d;
3. frads = 2.34567f;
4. d = Math.cos(frads); // Pass float to method
// that expects double

The float value in frads is automatically converted to a double value before it is handed to the cos() method. Just as with assignment conversions, strict rules govern which conversions are allowed and which conversions will be rejected by the compiler. The following code quite reasonably generates a compiler error (assuming there is a vector called myVector):

1. double d = 12.0;
2. Object ob = myVector.elementAt(d);

The compiler error message says, "Incompatible type for method. Explicit cast needed to convert double to int." This means the compiler can’t convert the double argument to a type that is supported by a version of the elementAt() method. It turns out that the only version of elementAt() is the version that takes an integer argument. Thus a value can be passed to elementAt() only if that value is an int or can be converted to an int.

11) The rules for object reference conversion can be stated as follows:

a) An interface type can be converted only to an interface type or to Object. If the new type is an interface, it must be a superinterface of the old type.

b) A class type can be converted to a class type or to an interface type. If converting to a class type, the new type must be a superclass of the old type. If converting to an interface type,the old class must implement the interface.

c) An array may be converted to the class Object, to the interface Cloneable or Serializable, or to an array. Only an array of object reference types can be converted to an array, and the old element type must be convertible to the new element type.


About the Authors

Philip Heller is a technical author, novelist, public speaker, and consultant. He has been instrumental in the creation and maintenance of the Java Programmer and Developer exams. His popular seminars on certification have been delivered internationally. He is also the author of Ground-Up Java (available from Sybex), which uses interactive animated illustrations to present fundamental concepts of Java programming to new programmers.

Simon Roberts worked for Sun Microsystems for nine years as an instructor, an authority on the Java language, and the key player in the development of the entire Java certification program. He is now a consultant and instructor, specializing in Java and security. He is also a flight instructor.

No comments: