Thursday, April 2, 2015

Interesting java codes

Most of the software engineers are living with java programs daily for their big projects. But the real fact is, we all are doing Copy Paste job, because mostly your working projects will be well designed. As we are very busy with Copy Paste job, some times we failed to understand the some basic interesting things in java. Some of us might be experienced with interesting things in java programs. I am sharing few things here which i have learned.

Some of us might though that, below code will not compile. But below code will compile and execute

     public class Curiosity {
           public static void main (String[ ] args) {
                System.out.println ("A curiosity");
                http://google.com
                System.out.println ("compile Ok!");
          }
     }
Output:
A curiosity
compile Ok!

Reason is simple, many of us aware of label statement in java. So here "http:" is label and // is comment line.

The next one is strange. Is is possible to call any method or field by null reference?. Most of us will give answer as NO. We have seen NullPointerException many times, so we thought that, below code will give run time exception. But below code will compile and execute successfully.

     public class Foo {
           static int fubar = 30;
           public static void main(String args[]) {
                Foo foo = null;
                System.out.println(foo.fubar);
           }
     }
Output:
30

Reason: Here, we have to notice variable declaration of "fubar". "fubar"is declared as static, so this will be stored in Method Area. Static variables are accessed via class and though the object is null, the type Foo is what is used to determine reference to the variable.

Friday, January 23, 2015

onbeforeonload event in firefox

There is a difference in onbeforeunload event across the version of Firefox browser. Normally we are using "alert" function to identify whether that particular function is calling or not. But in case of onbeforeunload event, alert will work upto firefox version 26.0. If the firefox version is greater than 26.0, alert funtion will not work. So if you want to use onbeforeunload event in firefox which is greater version of 26.0, then don't use alert function, just write your logic and test it.

<html> <head> <script> function onBrowserClose() { alert("Browser is closing"); // This will work, if your firefox version is less than or equal to 26.0 //Your logic } </script> </head> <body onbeforeonload="onBrowserClose()"> Browser close test... </body> </html>
If your Browse is latest version of firefox (greater than 26.0). Just remove the alert from above code.
<html> <head> <script> function onBrowserClose() { //Your logic } </script> </head> <body onbeforeonload="onBrowserClose()"> Browser close test... </body> </html>

Sunday, January 11, 2015

Java Exception Handling

An Exception is nothing but a problem which will occur during execution of program. An Exception can occur for many reasons. Below are the some of reasons,

  1. Invalid data from User
  2. Program try to read a file, but file is not available
  3. Program try to connect to Database, but credential is invalid

Exception Handling

Exception Handling is nothing but handle the Exception to avoid the abnormal termination of program. If an Exception is not handled, program will terminate abnormally.

To know the clear idea about an Exception handling, we will see the three categories of an Exceptions

  1. Checked Exceptions: Checked Exception is an Exception which can occur typically by user error. So programmer will not knowing it affront. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These Exceptions cannot be ignored by compiler. These kind of Exceptions are called "Checked Exception"
  2. Runtime Exceptions: Runtime Exception is an Exception which programmer can be knowing it affront based on critically of the code. Runtime Exceptions can be avoided during the development itself. But programmer is the responsible for that.
  3. Errors: There are not at all an Exceptions.The problem arise at beyond the user or programmer control. For example, JVM is running out of memory, this cannot be handled by program and cannot identify at compilation.

Exception Hierarchy

All Exceptions are subclasses of Exception class. Exception class is subclass of Throwable class. There is one more class which is derived from Throwable class is Error.


Catching an Exception

Exception handling is the combination of "try" and "catch" block. "try" will have the critical code which may arise an Exception. "catch" block should be written immediately next to "try" block. Whenever Exception is occurring inside the "try" block, control will go to respective "catch" block and execute the "catch" block.

     try {
           //critical code
     } catch(ExceptionName e) {
           //Handling block
     }

A catch statement involves declaring the type of exception you are trying to catch. If an exception occurs in critical code, the catch block (or blocks) that follows the try is checked. If the type of exception that occurred is listed in a catch block, the exception is passed to the catch block much as an argument is passed into a method parameter.

Single catch block - Sample

Following code is having an array with size 2, when try to add 3rd element, program will throws an Exception.

     try {
           int[] i = new int[2];
           i[0] = 1;
           i[1] = 2;
           i[2] = 3;
     } catch(ArrayIndexOutOfBoundsException e) {
           System.out.println("Exception thrown :" + e);
     }

Multiple catch block - Syntax and Sample

Syntax
     try {
           // Critical code
     } catch(ExceptionType e1) {
           // catch block 1
     } catch(ExceptionType e2) {
           // catch block 2
     } catch(ExceptionType e3) {
           // catch block 3
     }
Sample
     try{
           file = new FileInputStream(fileName);
           x = (byte) file.read();
     } catch(IOException i) {
           i.printStackTrace();
     } catch(FileNotFoundException f) {
           f.printStackTrace();
     }