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. 

Related Posts:

  • String Concatenation Operator ( + ) in Java In java " + " symbol is used to concatenate two strings where as in case numerals it will add up the values. It has wide use as an String concatenation. String concatenation means adding/ merging two or more than two st… Read More
  • Increments (++) and Decrements (--) Operators in Java This is most useful and confusing operator in java or any programming language. It is used to increase and decrease the value of a variable by 1. It is a Unary operator but the operand must be a variable containing num… Read More
  • Logical and bit-wise operators in java In java both logical and bitwise operators are used. Logical operator returns the true / false value depends on the condition check and bitwise return the same true / false depends on the condition check but the difference … Read More
  • Assignment Operator ( = ) in Java It is a kind of operator which has a wide use in any programming language and as it name suggest it is used to assign value to a variable and that value may be a direct value or a variable or we can say that it is used to a… Read More
  • Relational Operator in Java Relational Operator in Java is used where we use any comparison between two value or two variable or a value and a variable, but while comparing variables , it must some numeral value and hence all the operators used for … Read More

1 comment:

Blog Archive

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201976

Online Members

Live Traffic