Saturday, May 7, 2011

SUMMARY

Java is a modern object-oriented programming language. This chapter discussed how to compile and run
Java programs. We discussed the “primitive” data types in Java, as well as the frequently used “reference” types
including String and Array types. We explained the use of control structures for selective code execution
and iteration. These statements included the if-else, for, while, do-while, and switch statements.
Java takes advantage of the OO concept of classes, and the attendant principles of inheritance, encapsulation,
and polymorphism. An instance of a class is called an object, and objects have state and behavior. Classes define
both instance and static variables, which manifest the state of an object. Classes also define both instance and
static methods, which endow objects with their characteristic behaviors. Java also provides the concept of an
interface, which makes it possible to enforce similar behavior across different classes.
Error handling in Java is accomplished using Exception objects. When an error occurs, a program can
throw an Exception, which can describe the problem. Exceptions can be caught within the application
when the code generating the Exception is enclosed in a try block. Following the try block can be multiple
catch blocks and a finally block. Catch blocks can specify which subclasses of Exception they
will handle. Code in a finally block will always be executed, whether an error occurs in the try block or not.
If the application does not catch an Exception, the Exception will propagate up to the JVM, which will
handle the Exception by reporting it and terminating the program.
Input and output in Java occurs using streams. A stream is a sequence of data elements. The many types of
streams available in Java can be classified into Input/OutputStreams, which handle raw binary data as
bytes, and Reader/Writer streams which handle character data, and automatically perform translations to and
from character encodings. For efficiency, good programming practice is to wrap a base stream object in a
buffered stream to provide efficient buffering of I/O.
The Scanner class in Java is a handy class for reading text from the keyboard, a file, or other input stream.
The PrintWriter class is another convenience class that facilitates the common task of writing formatted
text to a file.

No comments:

Post a Comment