Special Operators in Java
An operator in Java is a special symbol. It is used to perform operations on two or more variables. We all know basic operations in Java. There are 8 types of operators in Java. They are
- Arithmetic operators
- Assignment operators
- Bitwise operators
- Unary operators
- Shift operators
- Logical operators
- Relational operators
- Ternary operators
Let us come to our main topic, which is Special operators in Java. There are two special operators in Java they are
- instanceof operator
- the dot operator (.)
Now let’s see what exactly, these operators are, and where they are used. And how they are used.
instanceof operator
This is the operator which depends on the object. It is a reference operator of an object. This operator gives only two outputs that are true or false. Many of the time we need to know whether a particular instance or a particular object belongs to a particular class or not. To do so, we should use the instanceof operator in Java. This operator gives output as true if the left hand side object is an instance of the given right hand side class.
To understand this let us consider this example,
Person instanceof student
Here a person is an object and a student is a class. Now, this operator will check whether the person object belongs to the student class. Then this operator will give output as true if the object person belongs to the student class. If it does not belong to the student class, then this operator returns false. That means it is used to check whether a particular object is an object or instance of a particular class. Another important point to note is if we apply the instanceof operator to a variable that is null, then this operator will return false as output.
Let us see the simple program to understand more about the instanceof operator in Java
InstanceOfExp1.java
class InstanceOfExp1{
public static void main(String args[]){
InstanceOfExp1 i1=new InstanceOfExp1(); // creating an instance or object for class
System.out.println(i1 instanceof InstanceOfExp1); // using the instanceof operator
}
}
Output:

Code Explanation:
Here in the above program, we have created a class InstanceOfExp1 and inside the main method, we have created an instance of an object i1 of the created class. Next in the output statement, we have used the instanceof operator to verify whether i1 is an instance or object to the class. Since it is an object to the class InstanceOfExp1 we got output as true.
An object of the child class is also an instance of the parent class. Because the child class inherits all the properties of the parent class.
Let us consider the program to understand briefly about this.
// Child1.java
class Parent{} // parent class
// child class
class Child1 extends Parent{ // inheriting properties of parent class
public static void main(String args[]){
Child1 c=new Child1(); // creating an instance or an object of class
System.out.println(c instanceof Parent); // using the instanceof operator
}
}
Output:

Code Explanation:
In the above code firstly, we have created a parent class Parent. Next, we have created the Child1 class which is the child class of the parent class. Now we have created an object or an instance c of the child class. Next in the output statement, we have used the instanceof operator to check whether object c is an instance of a class parent. As child class inherits the properties of the parent class. We will get output as true because c is also an instance of Parent.
As we have discussed above using the instanceof operator for a null value variable we will get output as false.
Let us understand the above statement using a program briefly.
InstanceOfExp2.java
class InstanceOfExp2{
public static void main(String args[]){
InstanceOfExp2 i2=null; // creating object and assigning null
System.out.println(i2 instanceof InstanceOfExp2); // using instanceof operator
}
}
Output:

Code Explanation:
In the above program first, we have created a class InstanceOfExp2. Inside the main method, we have created an instance of or an object to the class. We have assigned null to the created object. In the output statement, we have used the instanceof operator to check whether an object i2 is an instance of class InstanceOfExp2 . Since null is not an instance of the class we got output as false.
Dot (.) operator
Many of the time we use the dot operator in Java. This operator is also known as a member or separator or period. So, using this dot operator in Java we can access the instance variables of a class object or methods of a class object.
For example,
person.age
person.salary()
Here the person is an object and we have used the ( . ) dot operator is used to get the age of the person. And also, the salary of the person.
The dot ( . ) operator is also used to access sub packages from a package and classes from a package.
Let us understand more about this operator using a program.
DotOperatorExpl1.java
public class DotOperatorExpl1
{
void show()
{
double d = 101.4;
int i = (int)d; // converting double data type to integer data type
System.out.println(i);
}
public static void main(String args[])
{
DotOperatorExpl1 d = new DotOperatorExpl1(); // creating an instance or an object to the class
d.show(); // using dot ( . ) operator
}
}
Output:

Code Explanation:
In the above code firstly, we have created a class called DotOperatorExpl1.java. And inside the class, we have created a method called show(). Inside the show() method we have declared a variable that is of double data type. In the next line, we have converted the double data type variable to an int data type. And in the output statement, we wanted to get the I value. And inside the main method, we have created an instance or an object to the class. Next, we used the dot operator to display the show() method variables. So, we got 101 as the output.
Let us consider another program to get a brief knowledge of this operator.
DotOperatorExpl2.java
public class DotOperatorExpl2
{
public static void main(String args[])
{
int a = 54, b = 65;
DotOperatorExpl2 d = new DotOperatorExpl2(); // creating an instance for the class
int c = d.findMaximum(a, b); // using dot operator
System.out.println(" Maximum Value = " + c ); // printing maximum value
}
public static int findMaximum(int m, int n)
{
int maximum;
if (m > n)
maximum = m;
else
maximum = n;
return maximum;
}
}
Output:

Code Explanation:
In the above code, we have created a class called DotOperatorExpl2. In the main method, we have declared two variables a and b which are of integer data type. And, we have created an instance or an object d to the class. In the next step, we have used the dot ( . ) operator to call the method findMaximum(). This method returns the maximum value among the given two variables. Next in the findMaximum() function, we have used if-else blocks to find the maximum number among two numbers. Hence, we got the output as Maximum Value = 65.
This is all about special operators in Java. Hope you all understood about special operators.