×

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

  1. instanceof operator
  2. 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:

Special Operators in Java

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:

Special Operators in Java

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:

Special Operators in Java

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:

Special Operators in Java

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:

Special Operators in Java

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.


Related Topics

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

XOR of Array Elements Except Itself in Java

A lot of the biggest IT organisations, including The Google, Amazon, TCS, and The Accenture, etc., question about this issue in their interview processes. By addressing the issue, one hopes to assess...

5 minutes read.

Reverse a String using Collections in Java

Generally, a String is a grouping of characters. Yet, in Java, a String is an item that addresses a succession of characters. The Java.lang.String class is utilized to make a...

5 minutes read.

Hogben Numbers in Java

In this section, we will discover what the Hogben number is and develop Java programs that compute it. Java coding interviews and academic exams typically involve questions about the Hogben...

3 minutes read.

flour pack problem in Java

Create a method called canPack and give it three int parameters: bigCount, smallCount, and objective. The bigCount option indicates the number of large flour bags (5 kilos each). The smallCount argument indicates...

3 minutes read.

How to calculate time complexity of any program in Java

Java : Java's syntax and principles are derived from the C and C++ languages. We know that java is one of the programming language. The main feature of java which is...

3 minutes read.

Selection Sort in Java

Selection Sort in Java Selection sort is also a simple sorting algorithm that works by repeatedly finding the minimum element from the unsorted portion of the input array, and placing it...

5 minutes read.

Minimum Difference Between Groups of Size Two in Java

There is given an array with various integers in it. The goal is to divide the elements into distinct groups, each of which has just two, so that the difference...

3 minutes read.

Java String copyValueOf() method

copyValueOf() method returns a String that holds the character sequence of the character array. Syntax: copyValueOf(char[] data) Parameters: data : the character array i.e. String Returns: It returns a String that contains the characters of the...

2 minutes read.

Java Destructors

Before knowing about Java destructors, let us know about constructors. If we understand the concept of the constructor, we can easily understand about destructor and also, we will get an...

3 minutes read.

How to Create an API in Java?

Introduction The API can be abbreviated as Application Programming Interface. An API is a combination of set of classes and interfaces. It is also equivalent to a simple java program. To...

12 minutes read.

How to create a linked list in Java

Introduction: The linked listing is one type of linear statistics shaped like an array. Not like arrays, linked listing factors aren't stored in a contiguous place. The elements have linked...

3 minutes read.

Java Speech Recognition

The customer experience and convenience are improved with its utilization. Command and control interest in buying and voice synthesizers are supported by the cross-platform API defined by the API. For...

4 minutes read.

Copy data/content from one file to another in java

In this article, you will be acknowledged about how to copy data or content from one file to another file. Also, you will be acknowledged about the classes and methods...

3 minutes read.

What’s New in Java 15

Sealed classes are the new concept that was introduced by Java 15. Sealed classes are a preview feature. Most of the features which are released in java 15 are in...

3 minutes read.

Getting Synchronized Set from Java HashSet

The synchronizedSet() technique for java.util.Collections class is utilized to return a synchronized (string safe) set supported by the predetermined set. To ensure sequential access, it is important that everything admittance...

4 minutes read.

Union in Java

Sets.union() method in Java returns an immutable representation of both the union of two sets. Every element that is present in either backup set is included in the set that...

2 minutes read.

Zigzag Traversal of Binary Tree in Java

In this article, you will be acknowledged about the zigzag traversal of binary tree in java and the approaches or ways in which the zigzag traversal can be done. Zigzag Traversal A...

10 minutes read.

Java While Loop

A while loop is used to repeatedly execute a set of statements as long as its condition evaluates to true. This loop checks the condition before it starts the execution...

1 minute read.

New Features of Java 14

 Features like Switch expressions and text blocks which are previewed in Java 13 version are standardized in Java 14. Features in Java 14 Switch ExpressionText BlocksRecordsNull Pointer ExceptionsInstance ofPackaging ToolGarbage collectors 1.Switch Expressions Switch...

3 minutes read.