📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
obj instanceOf Object
instanceof Java Keyword Example 1
package com.javaguides.corejava.keywords.instanceofkeyword;
/**
* Class demonstrates the usage of instanceof keyword
* @author Ramesh Fadatare
*
*/
public class InstanceOfKeyword {
public static void main(String[] args) {
Pizza pizza = new VegPizza();
Pizza nonPizza = new NonVegPizza();
test(pizza);
test(nonPizza);
}
private static void test(Pizza pizza) {
if (pizza instanceof VegPizza) {
pizza.bake();
}
if (pizza instanceof NonVegPizza) {
pizza.bake();
}
}
}
interface Pizza {
public void bake();
}
class VegPizza implements Pizza {
@Override
public void bake() {
System.out.println("Bake Veg Pizza");
}
}
class NonVegPizza implements Pizza {
@Override
public void bake() {
System.out.println("Bake Non-Veg Pizza");
}
}
Bake Veg Pizza
Bake Non-Veg Pizza
private static void test(Pizza pizza) {
if (pizza instanceof VegPizza) {
pizza.bake();
}
if (pizza instanceof NonVegPizza) {
pizza.bake();
}
}
instanceof Keyword Example 2
package com.javaguides.corejava.keywords.instanceofkeyword;
public class InstanceOfKeyword2 {
public static void main(String[] args) {
Operations addOperation = new AddOperation();
Operations subOperation = new SubOperation();
Operations mulOperation = new MultiplyOperation();
Operations divOperation = new AddOperation();
Calculate calculate = new Calculate();
calculate.process(addOperation);
calculate.process(subOperation);
calculate.process(mulOperation);
calculate.process(divOperation);
System.out.println(addOperation instanceof Operations); // true
System.out.println(subOperation instanceof Operations); // true
System.out.println(mulOperation instanceof Operations); // true
System.out.println(divOperation instanceof Operations); // true
System.out.println(addOperation instanceof Object); // true
System.out.println(new AddOperation() instanceof Operations); // true
}
}
interface Operations {
public int doOperation(int num1, int num2);
}
class AddOperation implements Operations {
@Override
public int doOperation(int num1, int num2) {
return (num1 + num2);
}
}
class SubOperation implements Operations {
@Override
public int doOperation(int num1, int num2) {
return (num1 - num2);
}
}
class MultiplyOperation implements Operations {
@Override
public int doOperation(int num1, int num2) {
return (num1 * num2);
}
}
class DivisionOperation implements Operations {
@Override
public int doOperation(int num1, int num2) {
return (num1 / num2);
}
}
class Calculate {
public void process(Operations operations) {
if (operations instanceof AddOperation) {
operations.doOperation(1, 2);
}
if (operations instanceof SubOperation) {
operations.doOperation(2, 1);
}
if (operations instanceof MultiplyOperation) {
operations.doOperation(2, 2);
}
if (operations instanceof DivisionOperation) {
operations.doOperation(2, 1);
}
}
}
Java instanceof with an Array
// Java instanceof with array
int[] intArray = {
1,
2,
3,
4,
5
};
System.out.println(intArray instanceof Object);
true
String[] strArray = {
"ABC",
"XYZ"
};
System.out.println(strArray instanceof String);
String[] strArray = {
"ABC",
"XYZ"
};
System.out.println(strArray instanceof String[]);
Java instanceof keyword with Collection example
// Java instanceof with array
List < String > list = Arrays.asList("ABC", "XYZ");
System.out.println(list instanceof Collection);
true
All Java Keywords
- abstract Java Keyword
- assert Java Keyword
- boolean Java Keyword
- break Java Keyword
- byte Java Keyword
- case Java Keyword
- catch Java Keyword
- char Java Keyword
- class Java Keyword
- continue Java Keyword
- default Java Keyword
- do Java Keyword
- double Java Keyword
- else Java Keyword
- enum Java Keyword
- extends Java Keyword
- final Java Keyword
- finally Java Keyword
- float Java Keyword
- for Java Keyword
- if Java Keyword
- implements Java Keyword
- import Java Keyword
- instanceof Java Keyword
- int Java Keyword
- interface Java Keyword
- long Java Keyword
- native Java Keyword
- new Java Keyword
- package Java Keyword
- private Java Keyword
- protected Java Keyword
- public Java Keyword
- return Java Keyword
- short Java Keyword
- static Java Keyword
- strictfp Java Keyword
- super Java Keyword
- switch Java Keyword
- synchronized Java Keyword
- this Java Keyword
- throw Java Keyword
- throws Java Keyword
- transient Java Keyword
- try Java Keyword
- void Java Keyword
- volatile Java Keyword
- while Java Keyword
Comments
Post a Comment
Leave Comment