Sunday, December 16, 2012

Synchronous and Asynchronous Exceptions


     The interpreter executes the java program sequentially. An exception E can occur relative to a line (L) of program. That is that exception E will always occur at the execution of that line L. This is called Synchronous exception.

An asynchronous exception in java can occur at any point in the execution of a program.

Checked Vs Unchecked Exception

Checked Exception  in Java is all those Exception which requires being catches and handled during compile time. If Compiler doesn't see try or catch block handling a Checked Exception, it throws Compilation error. All the Exception which are direct sub Class of Exception but not inherit RuntimeException are Checked Exception.
IOException
SQLException
DataAccessException
ClassNotFoundException
InvocationTargetException

Unchecked Exception  in Java is those Exceptions whose handling is not verified during Compile time. Unchecked Exceptions mostly arise due to programming errors like accessing method of a null object, accessing element outside an array bonding or invoking method with illegal arguments. In Java, Unchecked Exception is direct sub Class of RuntimeException. What is major benefit of Unchecked Exception is that it doesn't reduce code readability and keeps the client code clean.
NullPointerException
ArrayIndexOutOfBound
IllegalArgumentException
IllegalStateException

Difference :- Checked Exception is required to be handled by compile time while Unchecked Exception doesn't.
Checked Exception is direct sub-Class of Exception while Unchecked Exception are of RuntimeException.
CheckedException represent scenario with higher failure rate while UnCheckedException are mostly programming mistakes.






Saturday, December 1, 2012

System.out.println


System – is a final class and cannot be instantiated. Therefore all its members (fields and methods) will be static and we understand that it is an utility class. System class are standard input, standard output, and error output streams.

out – is a static member field of System class and is of type PrintStream. Its access specifiers are public final. This gets instantiated during startup and gets mapped with standard output console of the host.

println – Method of PrintStream class. println prints the argument passed to the standard console and a newline. 

Can a top level class be private or protected


No. A top level class can not be private or protected. It can have either "public" or no modifier. If it does not have a modifier it is supposed to have a default access.If a top level class is declared as private the compiler will complain that the "modifier private is not allowed here". This means that a top level class can not be private. Same is the case with protected.

main() method in java


The main function is important function in Java programming language. 
The main method is the first method, which the Java Virtual Machine executes. When you execute a class with the Java interpreter, the run time system starts by calling the class's main() method. The main() method then calls all the other methods required to run your application. It can be said that the main method is the entry point in the Java program and java program can't run without this method.

Public static void main (String args[])
Public is an Access Specifier.

static is a keyword which illustrates that method shared along all the classes.
void illustrates that this method will not have any return type.
main is the method which string has an argument. 
Public specifier makes the method visible outside the Class and because of the static nature of the method, JVM can call this main method without instantiating the class.

ShareThis