java interview question from operators

java interview question from operators

Topic 

1-: new Vs newInstance()

2-:instanceof Vs isInstance()

3-:ClassNotFoundException Vs NoClassDefFoundException

                                                                new Vs newInstance()

new

  •     It is operator in java.
  • we can use a new operator to create objects if we know class name at the beginning.
        ex-:
            Student s=new Student();
            Test t=new Test();
  • In this case of new operator based on our requirements we can invoke any constructor 
         ex-:
            Test t=new Test();
            Test t1=new Test(10);
            Test t2 =new Test("Ram");
    

  • while using new Operator at run time if the corresponding .class file not available then we will get run time exception saying NoClassDefFoundError :Test
          ex-:     Test t=new Test();
          run  java Test123
                       at run time if Test.class file not available the we will get run time exception                                        NoClassDefFountError:Test



newInstance()

  • newInstance() is a method present in Class Class.
  • we can use newInstance() to create an Object if we don't know the Class name at the beginning and it is available dynamically at run time.
        Example-:
        /*
            class Student{
            }
            class Test {
            }    
            class Cat{
            public static void main(String[] args){
            Object o=Class.forName(args[0]).newInstance();
            System.out.print("Object is created of :"+o.getClass().getName());
            }
}
compile javac Cat.java
run  java Test
Output-: Object is created of : Test

compile javac Cat.java
run java Student
Output -:  Object is created of : Student
 
*/
  • newInstance() internally calls no-argment constructor hence to use newInstance() compulsory corresponding class should contain a no-argument constructor otherwise we will get a run time exception saying instantiationException.
  • while using newInstance() at run time if the corresponding .class file is not available then we will get run time exception saying classNotFoundExceiption.
    ex-:
    Object o=Class.forName(args[0]).newInstance();
           run -: java Test123
          at run time Test123.class file not available the we will get runtime exception saying            classNotFoundException:Test123


                                             classNotFoundException Vs noClassDefFoundError

For hardCoded class names,at run time if the corresponding .class file not available then we will get run time exception saying NoClassDefFoundError ,which is an unchecked exception.
ex-:
   Test t=new Test();
   java Test
   at run time if Test.class file is not available then we will get run time exception saying NoClassDefFoundError:Test

For dynamically provided class name at run time if the corresponding .class file not available then we will get run time exception saying classNotFoundException, which is a checked exception.

ex-:
   Object o=Class.forName(args[0]).newInstance();
   java Student
   at runtime, if Student.class file not available then we will get a runtime exception saying classNotFoundException:Student



                                                instanceof Vs isInstanceof()

instanseof is an operator in java we can use instanceof to check whether the given object is a particular type or not and we know the type at the beginning.
ex-:
   Thread t=new Thread();
   System.out.print(t instanceof Object);
   System.out.print(t instanceof Runnable);

isInstance() is a method present java.lang.Class we can use isInstance() to check whether the given object is a particular or not and we don't know the type at the beginning and it is available dynamically at run time.
isInstance() is method equivalent of instanceof Operator
ex-:
/*
   class Test{
     public static void main(String... args)throws Exception
     {
              Thread t=new Thread();
               System.out.print(Class.forName(args[0]).isInstance(t));
     }
     run java Test Runnable
     output : true
    run java Test String
    output:false
*/
Previous Post Next Post

Most Recent