Tuesday, November 23, 2010

Exceptions

An exception is a point in the code where something out of the ordinary has happened and the regular flow of the program needs to be interrupted; an exception is not necessarily an error. A method which has run into such a case will throw an exception using the throw (ExceptionClass) method. When an exception is thrown it must be caught by a catch statement that should sit right after a try statement.

How to handle an Exception?

To handle an Exception, enclose the code that is likely to throw an exception in a try block and follow it immediately by a catch clause.

Exception handling is a very powerful tool, you can use it to catch and throw exceptions and thus group all your error handling code very well and make it much more readable in larger more complex applications. After the try section there must exist one or more catch statements to catch some or all of the exceptions that can happen within the try block. Exceptions that are not caught will be passed up to the nexxt level, such as function that called the one which threw the exception, and so on.

try

{

// code that might fail

}

catch(Exception1ThatcanHappen E)

{

}

catch(Exception2ThatcanHappen E)

{

//things to do if this exception was thrown.

}

Java - Interfaces

An interface defines methods that a class implements. In other words, it declares what certain classes do. However an interface itself does nothing. All the action at least, happens inside classes. A class may implement one or more interfaces. This means that the class subscribes to the promises made by those interfaces. Since an interface promises certain methods, a class implementing that interface will need to provide the methods specified by the interface. The methods of an interface are abstract - they have no bodies. Generally a class implementing an interface will not only match the method specifications of the interface, it will also provide bodies - implementations - for its methods.

Example:
interface counting
{
abstract void increment();
abstract int getValue();
}

A class that implements a particular interface must declare this explicitly:
class ScoreCounter implements counting{
....
}

If a class implements an interface, an instance of that class can also be treated as though its type were that interface.

Java Arrays

An array is a group of variables that share the same name and are ordered sequentially from zero to one less than the number of variables in the array. The number of variables that can be stored in an array is called the array's dimension. Each element in the array is called an element of an array.

Declaring Arrays
When you declare an array variable you suffix the type with [] to indicate that this variable is an array.
Example: int [] k;

Allocating Arrays
When we create an array we need to tell the compiler how many elements will be stored in it. The numbers in the bracket specify the dimension of the array.

Initialising Arrays
Individual elements of the array are referenced by the array name and by integer which represents the position in the array. The number we use to identify them is called subscripts or indexes in a array. Subscripts are consecutive integers beginning with 0.

Java Methods

A method is a group of instructions that is given a name and can be called up at any point in a program simply by quoting that name.

You can write and call your own methods too. Methods begin with a declaration. Method consists of the following parts.
  1. Access Specifier
  2. public: This method can be called from anywhere.
    private: This method can be called or used within the class where it is defined.
    protected: This method can be used anywhere within the package in which it is defined.
  3. Static or Dynamic method
  4. Static methods have only one instance per class rather than one instance per object. All objects of the same class share a single copy of a static method. By default methods are not static.
  5. Return Type

Java Comments

Java programming supports three kinds of comments
  1. //
  2. compiler ignores everything from // to the end of the line.
  3. /* text*/
  4. compiler ignores everything from /* to */
  5. /** documentation */
  6. compiler ignores this kind of comment, just like it ignores comments that use /8 and *// The JDK javadoc tool uses doc comments when preparing automatically generated documentation..

Monday, November 1, 2010

Java-IO

1. Data in files on your system is called persistent data, because it persists after the program runs.
2. Files are created through streams in Java Code
3. A stream is a linear sequential flow of bytes of input and output data.
4. Streams are written to the file system to create files
5. Streams can also be transferred over the internet.
6. There are three types of streams
System.out -- Standard Output Stream
System.in -- Standard Input Stream
System.err -- Standard Error

Java.io package contains large number of classes that deal with Java input and output. Most of the classes consists of
  1. Byte Streams that are subclasses of InputStream and OutputStream
  2. Character streams that are subclasses of Reader and Writer


Reader and Writer classes read and write 16-bit Unicode characters. InputStream reads 8-bit bytes, while OutputStream writes 8-bit bytes.

If you use binary data, such as integers or doubles, then use the InputStream and OutputStream classes.
If you are using text data, then Reader and Writer classes are right.

Random Access

RandomAccessFile class permits random access. The data is stored in binary format. Using random access files improves performance and efficiency.

Object or Non-Object

If the data itself is an object, then use the ObjectInputStream and ObjectOutputStream classes.

Sources and sinks for data

You can Input or Output your data in number of ways: sockets, files, strings and arrays of characters.

Any of these can be a source for an InputStream or Reader or sink for an OutputStream or Writer.

Filtering

Buffering Instead of going back to operating system for each byte, you can use an object to provide a buffer.

Checksumming As you are reading or writing a stream, you might want to compute a checksum on it. A checksum is a value you can use later to make sure the stream was transmitted properly.

InputStream classes

In the InputStream class, bytes can be read from three different sources:
1. An array of bytes
2. A file
3. A pipe

Input Stream Methods

1. abstract int read() reads a single byte, an array, or a subarray of bytes. It returns the bytes read, the number of bytes read, or -1 if end-of-file has been reached.
2. read(), which takes the byte array, reads an array or a subarray of bytes and returns a -1 if the end-of-file has been reached.
3. skip(), which takes long, skips a specified number of bytes of input and returns the number of bytes actually skipped.
4. available() returns the number of bytes that can be read without blocking. Both the input and output can block threads until the byte is read or written.
5. close() closes the input stream to free up system resources.