Title: Java instanceof - 5% Transactions Silently Lost | TheCodeForge
Open Graph Title: Java instanceof - 5% Transactions Silently Lost | TheCodeForge
Description: 5% of payment transactions skipped silently because null passes instanceof returns false. Avoid ClassCastException by always checking null first.
Open Graph Description: 5% of payment transactions skipped silently because null passes instanceof returns false. Avoid ClassCastException by always checking null first.
Opengraph URL: https://thecodeforge.io/java/instanceof-operator-java/
Domain: thecodeforge.io
{
"@context": "https://schema.org",
"@type": "TechArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://thecodeforge.io/java/instanceof-operator-java/"
},
"headline": "Java instanceof - 5% Transactions Silently Lost",
"description": "5% of payment transactions skipped silently because null passes instanceof returns false. Avoid ClassCastException by always checking null first.",
"keywords": "Java instanceof - 5% Transactions Silently Lost, Java, Beginner, OOP Concepts, 5% of payment transactions skipped silently because null passes instanceof returns false. Avoid ClassCastException by always checking null first.",
"proficiencyLevel": "Beginner",
"author": {
"@type": "Person",
"@id": "https://thecodeforge.io/about/#naren",
"name": "Naren",
"url": "https://thecodeforge.io/about/"
},
"publisher": {
"@type": "Organization",
"@id": "https://thecodeforge.io/#org",
"name": "TheCodeForge",
"url": "https://thecodeforge.io",
"logo": {"@type": "ImageObject", "url": "https://thecodeforge.io/assets/og-image.png"}
},
"datePublished": "2026-03-17T00:00:00+00:00",
"dateModified": "2026-09-23T00:00:00+00:00",
"url": "https://thecodeforge.io/java/instanceof-operator-java/",
"inLanguage": "en",
"educationalLevel": "Beginner",
"image": "https://thecodeforge.io/assets/og-image.png",
"isPartOf": {"@id": "https://thecodeforge.io/#website"}
}
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{"@type": "ListItem", "position": 1, "name": "Home", "item": "https://thecodeforge.io/"},
{"@type": "ListItem", "position": 2, "name": "Java", "item": "https://thecodeforge.io/java/"},
{"@type": "ListItem", "position": 3, "name": "Java instanceof - 5% Transactions Silently Lost", "item": "https://thecodeforge.io/java/instanceof-operator-java/"}
]
}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "Can instanceof throw an exception?",
"acceptedAnswer": {
"@type": "Answer",
"text": "No. instanceof never throws at runtime. If the reference is null, it simply returns false. If there is a type mismatch the compiler would catch at compile time, you would get a compile error — not a runtime exception."
}
},
{
"@type": "Question",
"name": "What is the difference between instanceof and getClass() == ?",
"acceptedAnswer": {
"@type": "Answer",
"text": "instanceof returns true for the object's class AND all its supertypes. getClass() == SomeClass.class returns true only for the exact class, not subtypes. For most use cases instanceof is what you want. Use getClass() equality when you specifically need to exclude subclasses."
}
},
{
"@type": "Question",
"name": "Does instanceof work with generics?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Only partially. You can write obj instanceof List> but not obj instanceof List. Generic type information is erased at runtime — the JVM only knows it is a List, not what type it contains. The wildcard > is required to make the compiler accept the expression."
}
},
{
"@type": "Question",
"name": "What's the performance cost of instanceof?",
"acceptedAnswer": {
"@type": "Answer",
"text": "instanceof is a single bytecode instruction (instanceof) that the JVM executes quickly — typically about 1-2 nanoseconds. Pattern matching adds negligible overhead because the binding variable is a simple assignment. In most cases, the performance impact is immeasurable in application code."
}
},
{
"@type": "Question",
"name": "Can I use instanceof with array types?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Yes. For example, obj instanceof String[] works. Note that arrays are covariant in Java, so a String[] is also an Object[] and a Cloneable, among others."
}
}
]
}
| og:type | website |
| og:image | https://thecodeforge.io/assets/og-image.png |
| og:image:width | 1200 |
| og:image:height | 630 |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: max-image-preview:large