Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconSoftware Developmentbreadcumb forward arrow iconInstance of Operator in Java Explained for Beginners

Instance of Operator in Java Explained for Beginners

Last updated:
23rd Oct, 2022
Views
Read Time
7 Mins
share image icon
In this article
Chevron in toc
View All
Instance of Operator in Java Explained for Beginners

Object-Oriented Programming, also known as OOP, is a crucial programming paradigm, especially for aspiring software developers. OOPs work on several concepts like classes, objects, abstraction, polymorphism, abstraction, and inheritance. The idea is to bind data and different functions together to operate on it and abstract it from the end-user. 

Check out our free courses to get an edge over the competition.

Different object-oriented programming languages like Objective C, Java, Python, C++, and more operate on the core principles of OOPs. Apart from bundling data and functions together, the OOPs paradigm also allows developers the following clear benefits: 

  • OOPs, allow you to develop programs and applications using standard modules that communicate with one another. It eliminates the need to write the entire code from scratch, saving time. 
  • Using OOPs concepts, you can break your entire program into smaller, bite-sized chunks of modules that independently solve different problems and communicate with one another. That way, if one module goes bad, you can debug that part of the code, while the remaining part will keep executing normally. 
  • OOPs come with much more productivity within lesser time and development costs. 
  • Applications and tools developed using the OOPs paradigm are highly scalable and can be upgraded from small to large systems without much difficulty. 

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.

Ads of upGrad blog

When working with OOPs, instanceof is a concept one must be aware of to ensure a smooth learning experience.

The syntax of instanceof is -> <object_name> instanceof <class_name>

Instanceof is used to verify if a particular object is an instance in Java of a class, a subclass, or an interface. Essentially, you do it using an object whenever you instantiate a class. So, at a later stage, if you wish to find out the belongingness of an object, you can use the instanceof operator. It can also be referred to as a comparison operator because it compares the type with the instance. This operator returns either true or false values, and it is most commonly used in situations where you want to: 

  • Check whether any variable contains the instance of an object or not. 
  • Check if typecasting is valid or not. 
  • Downcasting as and when required. 

Check out upGrad’s Advanced Certification in DevOps 

Let’s try to understand instance of by looking at some examples and their execution!

Explore our Popular Software Engineering Courses

Examples of instance of using Java

Now that you have understood the basics of instance of operator and why it is used, let’s look at some examples to understand how it works better. 

Check the code snippet in the screenshot below: 

Examples of instanceof using Java

Here we are using the instance of operator to check whether the object ‘e’ is an instance in Java of Example1 class or not. You can see for yourself that object ‘e’ is present in our required class, which is why our program’s output returns true. Implying that object ‘e’ is an instance of Example1 class. 

Let’s now look at some more specific examples of using instance of in Java to understand the different scenarios and situations wherein you can use the instance of operator. 

Check out upGrad’s Advanced Certification in Cyber Security

We know that any subclass, by default, extends the parent class. Extending that logic, any object referenced in the subclass will also be an instance of the parent class through the concept of inheritance. Let’s see this in action using the instance of operator:

Examples of instanceof using Java

In the above example, the Mercedes class is an extension of the Car class, and as a result, Car becomes the parent class, whereas Mercedes is the child class. We are essentially creating a child class object and checking whether or not it is an instance of the parent class. As you can see, our program returns true as the output, which means that the child’s instance in Java is the instance of the parent class too. 

In-Demand Software Development Skills

Here is another example of instance of in Java where we use parent classes, subclasses, and objects, to see how the operator works and brings us the result: 

As you can see in the output section, we get a clear hierarchy of objects and get an idea of which object is an instance of which class by using our instance of operator. 

Instance of operator on a variable with Null value

So far, we have applied this operator on objects that were not null and got either true or false depending on whether they were the instance of the required class. But what happens if we apply the instance of operator to a variable containing a null value? Check the piece of code below: 

The program returns false as a result which implies applying instanceof operator to objects containing null values will return false => these objects will not be an instance of the class. 

Read our Popular Articles related to Software Development

Instance of operator on a variable with Null value Example

Instance of operators with different parent-child references

If we apply the instanceof operator on the parent object, it returns false. However, if we use a parent reference referring to a child, the result returned is true. Check out the below screenshot where instanceof has been applied in both these scenarios: 

Instance of operators with different parent-child references example

Instance of Java operator for downcasting

Downcasting, without a doubt, is one of the most important applications of the instanceof operator in Java. Downcasting refers to using a subclass to refer to the parent object. In simpler terms, downcasting is typecasting a parent’s object to a child’s object. Although the instanceof operator is not necessary to perform downcasting, using instanceof operator for downcasting ensures zero non-runtime or compile-time errors. Here is a code snippet where we perform downcasting in Java using the instanceof operator. 

Instanceof Java operator for downcasting example

In conclusion

The world of object-oriented programming and software development is full of insights, learnings, and new experiences. Instanceof operator is just one of the many operators you need to master to become comfortable with object-oriented programming. 

Ads of upGrad blog

We hope this article helped you understand the working of instanceof more lucidly. We suggest you take this hands-on by manually coding all the examples and playing around with them for different results, ensuring a better practical experience than theory. 

At upGrad, we understand the importance of practical, hands-on learning – especially when it comes to software development. As a result, our courses and training initiatives have practicality at their very core. One such initiative is Full Stack Development Bootcamp which will help you develop all the relevant skills required to excel in full-stack development. 

Check out the course page and get yourself enrolled soon! 

Profile

Pavan Vadapalli

Blog Author
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and long term technology strategy.

Frequently Asked Questions (FAQs)

1What is the syntax of instanceof Java operator?

The instanceof operator in Java uses the following syntax - instanceof

2What does the instanceof Java operator do?

The instanceof operator in Java finds out whether or not an object is an instance of a given class.

3What values does the instanceof operator return?

The instanceof operator in Java is a comparison operator, so it returns true or false as a result, depending on whether the object was the instance of the class or not

Explore Free Courses

Suggested Tutorials

View All

Suggested Blogs

Best Jobs in IT without coding
134249
If you are someone who dreams of getting into the IT industry but doesn’t have a passion for learning programming, then it’s OKAY! Let me
Read More

by Sriram

12 Apr 2024

Scrum Master Salary in India: For Freshers &#038; Experienced [2023]
900303
Wondering what is the range of Scrum Master salary in India? Have you ever watched a game of rugby? Whether your answer is a yes or a no, you might h
Read More

by Rohan Vats

05 Mar 2024

SDE Developer Salary in India: For Freshers &#038; Experienced [2024]
905050
A Software Development Engineer (SDE) is responsible for creating cross-platform applications and software systems, applying the principles of compute
Read More

by Rohan Vats

05 Mar 2024

System Calls in OS: Different types explained
5021
Ever wondered how your computer knows to save a file or display a webpage when you click a button? All thanks to system calls – the secret messengers
Read More

by Prateek Singh

29 Feb 2024

Marquee Tag &#038; Attributes in HTML: Features, Uses, Examples
5132
In my journey as a web developer, one HTML element that has consistently sparked both curiosity and creativity is the venerable Marquee tag. As I delv
Read More

by venkatesh Rajanala

29 Feb 2024

What is Coding? Uses of Coding for Software Engineer in 2024
5051
Introduction  The word “coding” has moved beyond its technical definition in today’s digital age and is now considered an essential ability in
Read More

by Harish K

29 Feb 2024

Functions of Operating System: Features, Uses, Types
5123
The operating system (OS) stands as a crucial component that facilitates the interaction between software and hardware in computer systems. It serves
Read More

by Geetika Mathur

29 Feb 2024

What is Information Technology? Definition and Examples
5057
Information technology includes every digital action that happens within an organization. Everything from running software on your system and organizi
Read More

by spandita hati

29 Feb 2024

50 Networking Interview Questions &#038; Answers (Freshers &#038; Experienced)
5138
In the vast landscape of technology, computer networks serve as the vital infrastructure that underpins modern connectivity.  Understanding the core p
Read More

by Harish K

29 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon