Java Corner

      Here you can find the things related to Java programming that can fetch the answers to
your java related queries.

Why cant you create an object for Math class???

     The Math class in java.lang package is defined with static methods. In general you can create an object for a class and use that object to access those methods. But you cannot create a object to access those methods that are defined in Math class. Have you ever tried???
Here is the answer for it!!!
     The Math class methods can be invoked without using object. It is because the inner implementation of Math class is as follows:
     public final class Math
    {
        private Math()
       {
        }
    }

i.e. The constructor of Math class is declared as private.  Therefore it is not possible to instantiate an object for the class which has a private constructor. This is the reason why you cant create an object for the Math class. And hence you can access the methods in the Math class simply by calling the method alone.
For example if you want find out the square root value of 16, you can do it as follows:
 e.g.

main()
{
      double numb=16.00;
      System.out.println(numb+"\n");
      double numb2=Math.sqrt(numb); //instead of object you have to use Math.sqrt() to call sqrt() method
      System.out.println(numb2);
}

XX------------------XX 

Have you ever wondered the Output of this scenario??

        String a=new String("D");
        String b=new String("D");
        boolean k=b.equals(a);      // Prints True        ---Statement 1
        System.out.println(k);
        System.out.println(a==b);  // Prints False       ---Statement 2

     Why this happens, when both a and b are string!!!

     This is because the statement 1( i.e. equals() method ) will just compare the content of the two strings whereas the second statement a= =b checks the reference(i.e. address of the two strings) also. As the string a and b pointing to two different string objects(since we declared new String( )), the reference differs for a and b and hence the result False.

XX--------------------XX

Can we catch multiple exceptions in a single catch statement???

        Currently (up to JDK 1.6), we cannot catch multiple exceptions in a single catch statement. But the latest version JDK 1.7 release supports this. We can catch multiple exceptions with pipe symbol as follows:
e.g.
catch(Exception1 obj1|| Exception2 obj2|| Exception3 obj3)
{
      //Statement goes here
}
In this example, the catch statement will catch all the three exceptions.

                                                           XX--------------------XX

(2nd Sep'11)
What happens when we clone a object using clone() method???

        clone() method is available in the Object class. This clone method can be used to make an exact copy of the object. It copies the entire fields i.e. state of the object. For an example consider an employee details wrapped up in an object named empObj.
        Lets assume that it has fields such empID, empDesignation, empDept, empSalary, etc. When we clone this object, using clone() method, it returns a local object say empObj1. This object contains all the fields that the empObj has, and when you make change in empObj1, it won't reflect in empObj. This is something like saving a word document in another name using Save as option. The further changes made will be reflected in newly saved document and the older one remains unchanged. Thus clone() method comes into picture, when we need a local object with few changes.


XX--------------------XX 
(28th Sep'11)

forName

What happens when Class.forName() method invoked while Database Connectivity???

     The forName() method defined in the class Class is a static method that returns an object of a class or interface that is given as string.  This method is equivalent to forName(String name, boolean initialize, ClassLoader loader). This method tries to locate the particular class or interface and then links and loads it. It takes the String as argument which is the class or interface name.
   For eg.
     Class.forName("com.mysql.jdbc.Driver"). Here the forName method loads the jdbc driver for connecting with the mysql database.
     Class.forName("oracle.jdbc.driver.OracleDriver"). Here the jdbc driver for Oracle will be loaded. 

XX--------------------XX

(28th Sep'11)

What happens when we use static block???

     A static block is the block of code that is declared with static keyword inside the braces { }. By doing so, the static blocks are executed immediately after instantiation of the particular class before executing any other methods in that class. 

For eg. consider two classes TestStatic which is a standalone class and another class called Main.
class TestStatic
{
    public void test( )
    {
        System.out.println("HAI");
    }
    static
    {
        System.out.println("1.....");
    }

    static
    {
        System.out.println("2.....");
    }
}//End of TestStatic
class Main
{

            public static void main(String ar[])
            {
                TestStatic t=new TestStatic( );
                t.test( );
            }

}// End of Main
     Here when the main program runs, the TestStatic class is instantiated and the static blocks in that class is executed before calling the test( ) method to execute. The order of execution of the static blocks occurs exactly in the order they appear in the source code.

No comments:

Post a Comment