new operator refers to the object creation where as instanceof used with the reference variable. Details of which we are going to discuss late in this topic. 

What is " new " Operator and its uses in Java ?

" new " is a keyword in java and it is used to allocate memory at the run-time and it the task of the JVM to allocate the memory or in other words we can say that it is used to create object which is responsible for allocating memory in java programming. 

Syntax for the new operator is :-

new (class_name)( [value] ) ;

The above statement indicates that with the help of new operator JVM is going to create a object of (class_ name) defined with it and that " value " indicates above is the way to pass the value while creating object to the constructor of its respective class. 

Another way of defining object by the use of new operator.

(class_name) (object_name) =new (class_name) ( [value] ) ;

In the above statement we are creating object through new operator and is the reference variable of that object and hence holds the reference value for that object. 

Here in the above syntax we are crating object of a user defined class that is why we are indicating (class_name) so that JVM should created object for that class only.

Reference value is used to indicate where the object is placed in the heap memory but don't misunderstood with this as an address because it is not a address indicating object but is a system generate random number as a reference value.

Note: we can create the object for any reference type variable like String, array etc .

What is " instanceof " Operator and its uses in Java
instanceof operator in java is only used for checking purpose whether a reference variable is holding the object / instance of specific type or not . It is a binary operator and it return only boolean value i.e true or false.


Conditions for using instanceof Operator

1> First operand must be reference type where as 2nd operand must be a reference type name ( class / interface ).

2> Type of reference variable and the specified type must be convertable and comparable i.e it will only allow to store the reference of its super type only.

for ex- Super class reference variable can hold the instance of its sub class but reverse it not true.
Inheritance property defines the convertibility. We cannot assign a instance of a class to the reference value of another class in a same level. 

1 comment:

Blog Archive

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic