Skip to main content

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
*/

Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Operators & Assignment part 3

                                                                                       Operators & Assignment 1-:instanceof Operators 2-:Bitwise Operators                                                                        instanceof Operators we can use instanceof operator to check whether the given object is of a particular type are not. Example-: /* list is a collection of objects. List l=new List(); l.add(Customer); l.add(Student); l.add(Test); Object o=l.get(...