Answers for "instanceof operator"

3

Explain about instanceof operator in java

An instanceof in Java is a comparison operator which, given an object instance, 
checks whether that instance is of a specified type (class/sub-class/interface) 
or not. Just like other comparison operators, it returns true or false.

Comparing any object instance with a null type through instanceof operator 
returns a false.
  
Instanceof operator is used to test the object is of which type.

Syntax : <reference expression> instanceof <destination type>
Instanceof returns true if reference expression is subtype of destination type.
Instanceof returns false if reference expression is null.
Posted by: Guest on November-30-2020
1

instanceof

function Phone(serial, price, color){
  this.serial = serial;
  this.price = price;
  this.color = color;
}

let phone1 = new Phone('abc1', 200, 'red');
let phone2 = new Phone('abc2', 400, 'green');

//instanceof 
console.log(phone1 instanceof Phone) // true

//constructor
console.log(phone1.constructor === Phone) //true
Posted by: Guest on July-24-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language