Tutorial Playlist
191 Lessons1. Introduction to Java
2. What is Java?
3. History of Java
4. Java Tutorial for Beginners
5. How Do Java Programs Work?
6. JDK in Java
7. C++ Vs Java
8. Java vs. Python
9. Java vs. JavaScript
10. From Java Source Code to Executable
11. How to Install Java in Linux
12. How to Install Java in Windows 10
13. Java Hello World Program
14. Structure of Java Program and Java Syntax
15. Operators in Java
16. Java If-else
17. Switch Case In Java
18. Loops in Java
19. Infinite loop in Java
20. For Loop in Java
21. For Each Loop in Java
22. Constructor in Java
23. Constructor Overloading in Java
24. Copy Constructor in Java
25. Default Constructor in Java
26. Parameterized Constructors in Java
27. Constructor Chaining In Java
28. Finalize Method in Java
29. Static Method in Java
30. Equals Method in Java
31. Abstract Method in Java
32. toString() Method in Java
33. Difference between equals method in Java
34. Inheritance in Java
35. Multiple Inheritance in Java
36. Hierarchical Inheritance in Java
37. Java Classes and Objects
38. Scanner Class in java
39. All classes in java are inherited from which class
40. What is Nested Class in Java
41. POJO Class in Java
42. Anonymous Class in Java
43. Final Class in Java
44. Object Class in Java
45. Packages in Java
46. Access Modifiers in Java
47. Static Keyword In Java
48. Final Keyword in Java
49. Checked and Unchecked Exceptions in Java
50. User Defined Exception in Java
51. Error vs. Exception in Java
52. Java Collection
53. Collections in Java
54. Garbage Collection in Java
55. Generics In Java
56. Java Interfaces
57. Functional Interface in Java
58. Marker Interface in Java
59. Streams in Java
60. Byte stream in java
61. File Handling in Java
62. Thread in Java
63. Thread Lifecycle In Java
64. Daemon Thread in Java
65. Thread Priority in Java
66. Deadlock in Java
67. String Pool in Java
68. Java Database Connectivity(JDBC)
69. Design Patterns in Java
70. Functional Programming in Java
71. OOP vs Functional vs Procedural
72. Heap Memory and Stack Memory in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
76. Hibernate Framework
77. JUnit Testing
78. How to Install Eclipse IDE for Java?
79. Command line arguments in Java
80. Jar file in Java
81. Java Clean Code
82. OOPs Concepts in Java
83. Java OOPs Concepts
84. Overloading vs Overriding in Java
85. Java 8 features
86. String in Java
87. String to int in Java
88. Why String Is Immutable in Java?
89. Primitive Data Types in Java
90. Non-Primitive Data Types in Java
91. This and Super Keyword in Java
92. HashMap in Java
93. Comparable And Comparator in Java
94. Type Casting in Java
95. Arrays Sort in Java with Examples
96. Variable Hiding and Variable Shadowing in Java
97. Enum in Java
98. Substring in Java
99. Pattern Programs in Java
100. Hashcode in Java
101. What is ByteCode in Java?
102. How To Take Input From User in Java
103. GCD of Two Numbers in Java
104. Linked List in Java
105. Arithmetic Operators in Java
106. Conditional Operators in Java
107. Stack and Queue in Java
108. Array Length in Java
109. Number Pattern Program in Java
110. Split in java
111. Map In Java
112. Difference Between Throw and Throws in Java
113. Difference Between Data Hiding and Abstraction
114. HashSet in Java
115. String Length in Java
116. Factorial Using Recursion in Java
117. DateFormat in Java
118. StringBuilder Class in java
119. Instance variables in Java
120. Java List Size
121. Java APIs
122. Reverse an Array in Java
123. StringBuffer and StringBuilder Difference in Java
124. Java Program to Add Two Numbers
125. String to Array in Java
126. Regular Expressions in Java
127. Identifiers in Java
128. Data Structures in Java
129. Set in Java
130. Pass By Value and Call By Reference in Java
131. Try Catch in Java
132. Bubble Sort in Java
133. Caesar Cipher Program in Java
134. Queue in Java
135. Object Creation in Java
136. Multidimensional Array in Java
137. How to Read a File in Java
138. String Comparison in Java
139. Volatile Keyword in Java
140. Control Statements in Java
141. Jagged Array in Java
142. Two-Dimensional Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
147. Matrix Multiplication in Java
148. Static Variable in Java
149. Event Handling in Java
150. parseInt in Java
151. Java ArrayList forEach
152. Abstraction in Java
153. String Input in Java
154. Logical Operators in Java
155. instanceof in Java
Now Reading
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
159. Stringtokenizer in java
160. Implementing and Manipulating Abs in Java
161. Char array to string in java
162. Convert Double To String In Java
163. Deque in Java
164. Converting a List to an Array in Java
165. The Max function in java
166. Removing whitespace from string in java
167. String arrays in Java
168. Strings in Java Vs Strings in Cpp
169. Sum of digits of a number in Java
170. Art of Graphical User Interfaces
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
175. Difference Between Java and Python
176. Square Root in Java
177. Reverse A String in Java
178. Even Odd Program in Java
179. Fibonacci Series in Java
180. Prime Number Program in Java
181. Java Program to Print Prime Numbers in a Given Range
182. Java Leap Year Program
183. Swapping of Two Numbers in Java
184. LCM of Two Numbers in Java
185. Math.sqrt() Function in Java
186. Area of Triangle in Java
187. Sort a String In Java
188. Factorial Program in Java
189. Javafx
190. Lambda expression in java
191. Setup Java Home and IDE on macOS
The instanceof operator uses a keyword to check for given reference objects in a referred variable. Hence, it is renowned as a comparison operator that performs a comparative operation between the instance and the type. The operation's return value is always displayed in Boolean true or false outcomes.
Java programs have featured the instance operator in several versions of the language, including instanceof Java 8, instanceof Java 17, and so on. It is prevalently similar to an is-a relationship depending on the implementation of an interface or the class inheritance of an object class.
The result upon using instanceof in Java is true if the object consists of an instance of the type. It yields similar outcomes when the object forms an instance of a subclass that originated from the type. The return value is true if the type is an interface and an object implements the interface.
The return values of applying instanceof operator in Java depend on the characteristics of the object and the type. Using instanceof to a parent object can make the outcome false, but applying it while referring to a child using the parent reference returns the result value as true.
This tutorial portrays a synopsis of the different techniques used in the instance method and the behaviors of instanceof in Java, along with providing an elaborate list of instanceof Java example scenarios.
The instanceof operator in Java helps determine an instance's specific category like class, interface, or subclass. An instanceof operator compares an instance with type, hence it is also called the type comparison operator. When a variable consists of a null value, its return value comes as false on using instanceof in Java.
It is quite similar to other comparison operators as its return values are also presented as true or false. The instanceof operator works using the foundation of is-a connections. An interface implementation or a class inheritance operation forms the basis of the concept of is-a connections.
public class upGradTutorials {
public static void main(String[] args) {
// Create objects of different classes
Animal animal = new Animal();
Dog dog = new Dog();
Cat cat = new Cat();
// Check if objects are instances of specific classes
System.out.println(animal instanceof Animal); // true
System.out.println(dog instanceof Animal); // true
System.out.println(cat instanceof Animal); // true
System.out.println(dog instanceof Dog); // true
System.out.println(cat instanceof Cat); // true
}
}
// Base class
class Animal {
}
// Derived classes
class Dog extends Animal {
}
class Cat extends Animal {
}
In this example, we have a base class Animal and two derived classes Dog and Cat. We create objects of these classes and then use the instanceof operator to check if the target object is the instance of a specific class.
We create instances of Animal, Dog, and Cat classes inside the main method. We then use the instanceof operator to check the relationship between objects and classes.
The instanceof operator returns true if the object is an instance of the specified class or any of its subclasses. In the example, the objects dog and cat are instances of both the Animal class and their respective derived classes (Dog and Cat).
In Java, the instanceof operator can also be used to check if an object is an instance of a specific class, even if the object is null. When null is used with the instanceof operator, it will always return false. This is because null does not refer to any object and, therefore, cannot be an instance of any class.
Here is an example:
public class upGradTutorials {
public static void main(String[] args) {
Animal animal = null;
System.out.println(animal instanceof Animal); // false
System.out.println(animal instanceof Dog); // false
System.out.println(animal instanceof Cat); // false
}
}
class Animal {
}
class Dog extends Animal {
}
class Cat extends Animal {
}
In this example, the variable animal is assigned null. When we use instanceof to check if animal is an instance of Animal, Dog, or Cat, all the expressions will return false since null is not an instance of any class.
Handling null values appropriately in your code to avoid any NullPointerException errors when working with objects is important.
In Java, downcasting is the process of casting an object reference of a superclass to its subclass type. The instanceof operator is commonly used with downcasting to ensure type safety before performing the cast.
Here is an example:
class Animal {
public void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
public void bark() {
System.out.println("Dog is barking");
}
}
class Cat extends Animal {
public void meow() {
System.out.println("Cat is meowing");
}
}
public class upGradTutorials {
public static void main(String[] args) {
Animal animal1 = new Dog();
Animal animal2 = new Cat();
// Downcasting with instanceof and explicit casting
if (animal1 instanceof Dog) {
Dog dog = (Dog) animal1;
dog.bark();
}
if (animal2 instanceof Cat) {
Cat cat = (Cat) animal2;
cat.meow();
}
}
}
In this example, we have a superclass Animal and two subclasses, the Dog and the Cat. We create instances of Dog and Cat and assign them to variables of type Animal. We can then use the instanceof operator to check if the object is an instance of a specific subclass before downcasting.
Using instanceof, we verify if animal1 is an instance of Dog and animal2 is an instance of Cat. If the conditions are true, we can safely downcast the objects to their respective subclass types (Dog and Cat) and invoke subclass-specific methods (bark() and meow()).
Using instanceof before downcasting helps prevent ClassCastException errors by ensuring that the object is of the expected type before performing the cast.
In Java, downcasting without the instanceof operator can lead to runtime exceptions if the object being cast is not an instance of the target class or interface. However, if we are confident about the object type, we can perform the downcast directly.
Here is an example:
In this example, we have a superclass Animal and a subclass Dog. We create an instance of Dog and assign it to a variable of type Animal. Since we are confident that the object is a Dog, we can directly downcast the animal object to type Dog using parentheses and invoke the bark() method.
However, it's important to exercise caution when performing downcasting without instanceof, as it bypasses type checking at compile-time. If the object being cast is not an instance of the target class, a ClassCastException will occur at runtime. It's generally recommended to use instanceof to perform type checks before downcasting to ensure type safety.
The data in a parent class member can be accessed using references from the parent type while referring to the child objects. Typecasting allows access to the child data members, where the validity issue can be resolved through the instanceof string Java operator. Commonly applied methods of Java instanceof meaning to improve work efficiency can be enlisted as follows:
class Parent {
}
class Child extends Parent {
}
public class upGradTutorials {
public static void main(String[] args) {
Parent parent = new Parent();
// Check if the parent object is an instance of the child class
System.out.println(parent instanceof Child); // false
}
}
In this example, we have a parent class Parent and a child class Child. We create an instance of Parent and assign it to a variable of type Parent. When we use the instanceof operator to check if the parent object is an instance of the Child class, it returns false because it is not an instance of the child class.
class Parent {
}
class Child extends Parent {
}
public class upGradTutorials {
public static void main(String[] args) {
Parent parent = new Child();
// Check if the parent reference referring to a child is an instance of a child
System.out.println(parent instanceof Child); // true
}
}
Like the previous example, we have a parent class Parent and a child class Child. Similarly, We create an instance of Child and assign it to a variable of type Parent. When we use the instanceof operator to check if the parent reference referring to a child object is the instance of the Child class, it returns true because the parent reference refers to a child object.
class Parent {
}
class Child extends Parent {
}
public class upGradTutorials {
public static void main(String[] args) {
Parent parent = new Parent();
// Check if the parent reference is an instance of Parent and Child
System.out.println(parent instanceof Parent); // true
System.out.println(parent instanceof Child); // false
}
}
In this example, we have a parent class Parent and a child class Child. We create an instance of Parent and assign it to a variable of type Parent. The parent reference is referring to a Parent object. Using the instanceof operator, we check if the parent reference is an instance of the Parent and Child classes. The check for Parent returns true because the parent reference is an instance of the Parent class. However, the check for Child returns false because the parent reference is not an instance of the Child class.
class Parent {
}
class Child extends Parent {
}
public class upGradTutorials {
public static void main(String[] args) {
Parent parent = new Child();
Child child = new Child();
// Check if the parent reference and child reference are instances of Parent and Child
System.out.println(parent instanceof Parent); // true
System.out.println(parent instanceof Child); // true
System.out.println(child instanceof Parent); // true
System.out.println(child instanceof Child); // true
}
}
The primary utility of the instanceof comparison operator in Java is in determining the distinct type of an object instance, including class, subclass, and interface as the basic categories. It is quite similar to the other comparison operations as instanceof in Java also returns the values in true or false.
Another name for the instanceof operator is a type comparison operator which can perform downcasting without any possible runtime exceptions. If you want to learn more about Java programming, you can opt for courses offered by upGrad. upGrad presents several online courses that can teach you how to use instanceof in Java and numerous other topics.
Instance variables in Java can be defined as class property that varies from object to object. It can be created by clearing the variables in the class definition. The functionality of instanceof in JavaScript permits the creation of a copy of the data for every object value.
The word 'polymorphism' stands for 'multiple forms'. Inheritance of a class in Java allows modifications of the attributes and methods using another class, while polymorphism utilizes these methods to conduct numerous tasks. Polymorphism in Java might occur if several interrelated classes are connected by inheritance.
A process that works with the instances of a class and not the class itself is known as the instance method. An instance method can be defined by simply removing static from the method heading. The variables and methods within the method definition can be referred to by using their names and omitting the dots.
PAVAN VADAPALLI
Director of Engineering
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...