Java Miscellaneous Operators

Java Miscellaneous Operators

Java's miscellaneous operators are: ternary operator, member access, comma, array index, new, instanceof, and typecast. These operators are explained one by one in following sections.

Java Ternary or Conditional Operator

Java provides a special operator that is called ternary or conditional operator. This operator is a set of two symbols that are ? and :. Both symbols collectively form the conditional operator. This operator can be used if we have to initialize or assign a variable on basis of some condition. It follows the below syntax.

expr-1 ? expr-2 : expr-3;

In above syntax, expr-1 can be any expression that returns a boolean value. If expr-1 returns true then expr-2 is processed; else expr-3 is processed. Note that both expr-2 and expr-3 must return the same type of value and they cannot be void. Following piece of code demonstrates the use of Java ternary operator.

/* TernaryOperatorDemo.java */
public class TernaryOperatorDemo
{
    public static void main(String[] args)
    {		
        int n = 10;
        boolean flag = (n % 2 == 0 ? true : false ); 
        System.out.println(n + " is even? " + flag);
    }
}
 
OUTPUT
======
10 is even? true

Java Member Access Operator

The Java member access operator is a dot (.) symbol that is used to access data members and member methods of a class by its objects.

Java Comma Operator

Java comma operator is a ',' sign that is used to separate function arguments, and to declare more than one variable of same type in one statement.

Java Array Index Operator

Java array index operator, a set of square brackets ([]), is used to declare and access array elements.

Java new Operator

The Java new operator is used to create a new object. Operator new is a Java keyword. It is followed by a call to a constructor, which initializes the new object. Note that declaring an object and creating an object are two different things. Simply declaring a reference variable does not create an object. For that, we need to use the new operator. The new operator creates an object by allocating memory to it and returns a reference to that memory location. The Java new operator needs a single, postfix argument: a call to a constructor. The name of the constructor provides the name of the class to instantiate. Following piece of code demonstrates the use of new operator.

/* NewOperatorDemo.java */
class Universe
{
  public Universe() {}
  public void myUniverse()
  {
    System.out.println("This is my Universe");
  }
}
 
public class NewOperatorDemo
{
  public static void main(String[] args)
  {
    //new operator creates a new object of type Universe
    //and assigns it to reference newUniverse
    Universe newUniverse = new Universe();
    newUniverse.myUniverse();
  }
}
 
OUTPUT
======
This is my Universe

Java instanceof Operator

Java instanceof operator also called type comparison operator compares an object to a specific type. It follows the syntax objRef instanceof type. Here objRef is the object name and the type is the name of object type whom objRef will be compared to. The equals() method of Java is a nice example that uses instanceof operator to check if two objects are equal. The following example (InstanceOfDemo.java) shows the use of Java instanceof operator.

/* InstanceOfDemo.java */
 
class Universe
{
  public Universe() {}
  public void myUniverse()
  {
    System.out.println("This is my Universe");
  }
}
 
public class InstanceOfDemo
{
  public static void main(String[] args)
  {
    Universe newUniverse = new Universe();
    newUniverse.myUniverse();
    System.out.println("Is newUniverse an object of Universe? " + (newUniverse instanceof Universe));
    System.out.println("Does newUniverse inherit Object? " + (newUniverse instanceof Object));
  }
}
OUTPUT
======
This is my Universe
Is newUniverse an object of Universe? true
Does newUniverse inherit Object? true

Java has a rich set of operators. But, like C and C++, Java does not support operator overloading. You can also note that most of the operators of Java are applied on basic types only and not on objects. To perform any operation on objects Java believes in using member methods that's because of object orientation approach of Java to problem solving.

Last Word

In this tutorial we explained miscellaneous Java operators. Hope you have enjoyed reading this tutorial. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!

References




Get Free Tutorials by Email

About the Author

is the main author for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.

Today's Tech News

WannaCry ransom notice analysis suggests Chinese linkPosted on Monday May 29, 2017

Researchers say the WannaCry ransom note was poorly translated - possibly using Google Translate.

BA boss 'won't resign' over flight chaosPosted on Monday May 29, 2017

Chief executive Alex Cruz says flight disruption at Heathrow and Gatwick had nothing to do with cost cutting.

Hay Festival 2017: Stephen Fry's warning for the webPosted on Sunday May 28, 2017

Delivering a lecture at the Hay Festival, the actor said society could face "dire consequences".

Courtesy BBC News