PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Java · Theory

Java instanceof Keyword

← All stacks

Theory

43/270

Java instanceof Keyword

instanceof checks: is this object an example of that type? Expression: obj instanceof Type → true or false. Use it before a cast so you avoid ClassCastException.

It understands inheritance: Dog instanceof Animal can be true. null instanceof Anything is always false — a favourite MCQ.

Show Animal a = new Dog(); then a instanceof Dog and a instanceof Animal. Don’t cast blindly in uncertain hierarchies.

Let's take this on the board with one tiny Main class — no extra files. Java instanceof Keyword — output: true then true. d is a Dog, and because Dog extends Animal, d instanceof Animal is also true. Start from main, go line by line, and stop at each print. That output is the proof for instanceof Keyword.

Exam tip

null instanceof X is false — common MCQ.

Example

class Animal {}
class Dog extends Animal {}
public class Main {
  public static void main(String[] args) {
    Animal a = new Dog();
    System.out.println(a instanceof Dog);
    System.out.println(a instanceof Animal);
  }
}

Java instanceof Keyword — output: true then true. d is a Dog, and because Dog extends Animal, d instanceof Animal is also true.

Short notes

  • Definstanceof checks if an object is that type. Returns true/false.
  • RuleUse before casting to avoid ClassCastException.
  • RememberWorks with inheritance. Dog instanceof Animal can be true.
  • Trapnull instanceof X is always false.

Questions

1

What does instanceof return?

2

Why use instanceof before cast?

3

What is null instanceof String?

Previous← Static and Dynamic Binding in JavaNextAbstract class in Java →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.