×

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

How to uninstall the Java in Ubuntu

We need java on our computers in our daily lives because there are lots of different applications that are developed using the java environment, so we have to install java...

4 minutes read.

Difference between C, C++, java

C Language: C language is a procedure oriented language. It has been invented by Dennis Ritchie in the year 1970. It is one of the computer programming language. The purpose of...

3 minutes read.

Java Null Keyword

Null is a term that is only used for literal values in Java. Although it appears to be a term, it is a literal opposite of true and false. Java's...

3 minutes read.

Missing Number in an Arithmetic Progression in Java

Given an array that shows the elements of an orderly arithmetic progression. Find the missing number to complete the succession of elements. Example:  Input: a [ ] = {2 , 4 , 6...

3 minutes read.

Math fma () method in java

In java, the Math module constitutes of fma () Method in it. This method can be accessed in two ways which can be differentiated by the parameters which are given...

4 minutes read.

Java String toLowerCase() methods

Java String toLowerCase() method is used to convert all the characters of the String into lower case. Syntax: public String toLowerCase()              public String toLowerCase(Locale locale) Returns: It returns Lower...

1 minute read.

Java finally

Java finally: There are some statements in a program whose execution is extremely important. For example, closing of a database connection. Statements that do the closing of the database connection...

5 minutes read.

How to Change the Day in the Date using Java?

To operate with the date and time in Java, we need the Calendar abstract class. It provides a number of helpful interfaces that enable us to convert dates between a...

4 minutes read.

Java Lock

A lock is indeed a threaded synchronization technique similar to Java's synchronized blocks, however, locking can be more complex. It's not like we can completely get rid of the synchronized...

5 minutes read.

Quick Sort in Java

Quick Sort in Java Like merge sort, quick sort also uses the divide and conquer approach to sort the given array or list. In quick sort, the sorting of an array...

6 minutes read.

Java Integer toUnsignedString() method

The toUnsignedString() method of Java Integer class returns a string representation of the argument as an unsigned decimal value. The second syntax returns a string representation of the given argument as...

2 minutes read.

Java Private keyword

A Java access modifier is a private keyword. It can be used to inner classes, methods, and variables. It is the type of access modifier that is most constrained. Privately declared...

4 minutes read.

Shopping Bill in Java

Java Shopping Bill Here in this program, we are about to create a JAVA class called Products which will have some properties or attributes like prod_name (Product Name ), qty (quantity),...

4 minutes read.

Java Program to remove duplicate characters in a string

In strings, we can find many characters present one or more times in a string; accessing a particular character is difficult due to repetition. Duplicate characters will present in the...

5 minutes read.

Java String charAt() method

It returns the char value present in the string at the specified index. Here, index value can not be greater than length() -1. Syntax: public char charAt (int index) Parmeters It accepts only...

3 minutes read.

What is String in Java?

What is String in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a Java...

4 minutes read.

Java ResultSetMetaData

The data about another data is called Metadata. The ResultSetMetaData is used to store the data about ResultSet. The ResultSet Contains the columns, rows, names of table, datatypes etc. these...

2 minutes read.

Java Math scalb() Method

The scalb() method of Java Math class returns a single perfectly rounded product of floating-point and member of the double value set as if performed by d*2scaleFacor rounded value. Syntax: public static...

2 minutes read.

Java Get File Size

There are three ways to get the file size in Java. Using Java InputOutput Using NewInputOutput Library Files.size() Method FileChannel.size() Method Using Apache Commons InputOutput Using Java InputOutput: The Java IO package offers the classes that deal...

5 minutes read.

Stack vs Heap in Java

In Java, whenever we declare an object or create a variable, whether it is an instance variable, local variable, or static variable, a certain memory is used to store the...

4 minutes read.