×

Misc Operators in Java

In this article, we are going to learn about the misc operators in Java. Misc operators are nothing but the miscellaneous operators. The java programming language supports some of the miscellaneous operators in Java.

Introduction

The very first operand will always be examined first in the conditional operator known as the misc operator, which has three operands. If the result is nonzero, the next operand is evaluated to determine its value. If not, the result's value is determined by evaluating the third operand.

There are many miscellaneous operators. They are:

  • Ternary or conditional operator
  • Member access operator
  • Comma operator
  • Array Index operator
  • New operator
  • Instanceof Operator

Let us know about each operator discussed above.

Ternary or Conditional operator

The ternary or otherwise conditional operator is a unique operator that Java offers. The pair of symbols that make up this operator are ‘?’ and ‘: ‘. Together, the two symbols make up the conditional operator. If we need to initialize or allocate a variable based on a condition, we can use this operator. It uses the syntax listed below.

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

Any expression that produces a Boolean value can be used in the syntax above as expr-1. If expr-1 returns true, expr-2 is then processed; if not, expr-3 is. It should be noted that expr-2 and expr-3 cannot return void values and must both return same type of value. The use of the Java ternary operator is demonstrated in the following line of code.

Let us understand it with a simple example program

File name: Ternary.java

public class Ternary
{
    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

Member Access Operator

Dot (.) symbols are employed as the Java member accessibility operators to allow objects to access a class's instance variables and member methods.

Let us understand it with an example

File name: Maccess.java

public class Maccess {
    public static void main(String[] args) {
        Position p = new Position(0,0);
        int a = p.x; // example of member access
    }
}


class Position {
    public int x;
    public int y;


    public Position(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

Output

12
dash: 2: 12: not found

Comma Operator

The "," symbol, known as the comma operator in Java, is used to demarcate function parameters and to define multiple variables of the same type in a single statement.

Let us understand it with an example

File name: Comma.java

class Comma
 { 
             public static void main(String args[] ) 
             { 
                         int i,j; 
                         for(i=1,j=20;j>=10;i++,j-=2) 
                         { 
                                     System.out.println(i+"  "+j); 
                         } 
             } 
 }

Output

1  20
2  18
3  16
4  14
5  12
6  10

Array Index Operator

The square bracketed ([]) Java array search operator is used to define and retrieve array elements.

Let us understand it with a simple example program.

File name: Aindex.java

// A Java program that creates an array of integers, inserts some values into the array, and //outputs each value to serial monitor is used to demonstrate the concept.
class Aindex {
public static void main(String[] args)
{
// establishes an integer array.
int[] arr;
arr = new int[5];
 
arr[0] = 11;
arr[1] = 21;
arr[2] = 31;
arr[3] = 41;
arr[4] = 51;
// generally referring to the array's specified elements
for (int i = 0; i < arr.length; i++)
System.out.println("component at index " + i+ " : " + arr[i]);
}
}

Output

component at index 0 : 11
component at index 1 : 21
component at index 2 : 31
component at index 3 : 41
component at index 4 : 51

New Operator

To establish a new object in Java, use the new operator. Java's operator new keyword. A call to the constructor, which initializes the new object, comes next. Keep in mind that creating an object and declaring an object seem to be two distinct processes. An object cannot be created by merely declaring a reference variable. We must employ the new operator in order to do that. By allocating memory for an object, the new operator constructs it and then returns a handle to that main memory. A call to a constructor is the only postfix parameter required for the Java new operator. The identity of a class to initialize is provided by the constructor’s name. The use of the new operator is demonstrated in the following line of code.

Let us understand it with an example

File name: New.java

class New
{
  public Universe() {}
  public void myUniverse()
  {
    System.out.println("This Universe is limitless.");
  }
}
  public static void main(String[] args)
  {
// A new instance of type Universe is created by the new operator and assigned to    //reference  n
    Universe n = new Universe();
    n.myUniverse();
  }
}

Output

This universe is limitless.

Instanceof Operator

An object is compared to a certain type using the Java instanceof operator, commonly known as the type comparison operator. It uses the objRef instanceof type syntax. Here, the type seems to be the name of the object type to which the object named objRef will be compared. Java's equals() function is a good illustration of how the instanceof operator can be used to figure out whether two objects be equal. The use of the Java instanceof operator is demonstrated in the example code below (InstanceOfDemo.java).

Let us understand it with an example program

File name: Instanceof.java

class Instanceof
{
  public Universe() {}
  public void myUniverse()
  {
    System.out.println("This Universe is limitless.");
  }
}
{
  public static void main(String[] args)
  {
    Universe n = new Universe();
    n.myUniverse();
    System.out.println("Is n an object of Universe? " + (n instanceof Universe));
    System.out.println("Does n inherit Object? " + (n instanceof Object));
  }
}

Output

This Universe is limitless.
Is n an object of Universe? True
Does n inherit Object? True

Conclusion

The operator library in Java is extensive. However, Java somehow doesn't support operator overloading, just like C and C++ do. Additionally, you may have noticed that the majority of Java's operators only apply to fundamental types, not objects. Because Java takes an object-oriented approach to problem solving, it argues that member methods should be used to perform any activity on objects.


Related Topics

Factorial of a Large Number in Java

We've already talked about a number's factorial. We must still go over the factorial of a large number separately, though. The method used to determine the factorial of a small...

8 minutes read.

How to Convert int to char in Java

To convert a higher data type to lower data type, we need to do typecasting. Casting is also required when we want to convert ASCII value into character. It is...

3 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

Difference between Abstract Class and Interface

There is a similarity between abstract class and interface is that we cannot create objects for both of them. But irrespective of this, there are some differences between them, let’s...

2 minutes read.

Binary Search Java

Binary search is a search mechanism for key elements from the given List/Array. In Binary search, the search mechanism is followed by dividing the array into parts; hence the search...

3 minutes read.

Encapsulation Program in Java

Encapsulation Program in Java Encapsulation program in Java demonstrates the technique to bind methods and fields in a single unit. The term encapsulation is inspired by the word ‘capsule’, which is...

3 minutes read.

Java IO file not found exception

One of the exception classes offered by the java.io package is FileNotFoundException. An exception is raised when we attempt to access a file that isn't present in the system. It...

4 minutes read.

Can Abstract Classes have Static Methods in Java

Abstract Class An abstract class in Java is one that explicitly uses the keyword "abstract" in its declaration. There are options for both non-abstract and abstract techniques (method with the body)....

4 minutes read.

Convert Char array to string in java

A collection of characters is referred to as a string. A character array differs from a string in that the string is canceled by the special character "\0." A string...

4 minutes read.

String isnullorempty in Java

What do you mean by String? 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...

4 minutes read.

How to convert float to String in Java

How to convert float to String in java There are following methods to convert float to String: Convert using Float.toString(float) Convert using String.valueOf(float) Convert using Float.toString(float) The Float class has a static method that returns...

2 minutes read.

java.lang.NumberFormatException for Input String

java.lang.NumberFormatException for Input String The exception java.lang.NumberFormatException for input string occurs when we try to convert a string into a number format. For example, if someone converts the string “Tutorial & Example”...

3 minutes read.

Java instance variable

An instance in object-oriented programming (OOP) is a particular implementation of any object. Each realized version of an item, which might vary in several ways, is an instance. Instantiation is...

3 minutes read.

Crown Pattern in Java

We know the importance of solving pattern problems. We can solve pattern problems by using any programming language. There is no rule to translating into a particular programming language. We...

4 minutes read.

Java Integer parseUnsignedInt() method

The parseUnsignedInt() method of Java Integer class parses the string argument as an unsigned decimal integer. The second parameter parses the string argument as an unsigned integer in the radix specified...

2 minutes read.

Untouchable Number in Java

If a number N cannot be divided properly by any positive number, it is said to be an untouchable number. Additionally known as nonaliquot numbers. The sequence is A005114 from...

3 minutes read.

How to Convert char to String in Java

How to Convert char to String in Java There are two methods to convert char to String: Using String.valueOf(char) method Using Charcter.toString(char) method Using String.valueOf(char) method valueOf(char) is the static method of String class that...

2 minutes read.

Mutable and Immutable in Java

Java is a programming language in which everything is treated as an object. Its procedures and functions are centred around objects because it is an object-oriented programming language. Mutable and...

6 minutes read.

String Manipulation in Java

String Manipulation in Java In Java, string manipulation is a common task performed by programmer. Java String class provides many built-in functions that are used to manipulate string. The manipulation of...

4 minutes read.

Java Wrapper Class

Wrapper class in Java encapsulates a primitive type within an object. In other words, it is a unique mechanism of Java to convert primitive type in to object and object into primitive type....

3 minutes read.