Miscellaneous Operators

These little-known but incredibly useful operators, various in Java can improve your coding efficiency and open doors to new opportunities. the miscellaneous operator refers to a group of operators that do not fit into the categories of arithmetic, relational, logical, or bitwise operators. These miscellaneous operators offer additional functionality and convenience in Java programming, making code more expressive and concise.

Here are the types of miscellaneous operators in Java:

1. Ternary Operator (Conditional Operator ?:):

Definition: With the ternary operator, you can write an if-else formula on a single line. It checks a boolean expression to see if it is true or false and gives one of two values.

Explanation: The ternary operator has the syntax condition? value_if_true : value_if_false. If the condition is true, the value of value_if_true is returned; otherwise, the value of value_if_false is returned.

Example:

public class OperatorExample{
   public static void main(String[] args) {
    	int stu_age = 43;
    	String result = (stu_age >= 18) ? "Adult" : "Minor";
    	System.out.println("Result: " + result); // Output: Result: Adult
    
   }
}

Output:

Result: Adult

2. Instanceof Operator:

Definition: The instanceof operator is used to check whether an object is an instance of a specific class or implements a particular interface.

Explanation: It returns a boolean value indicating whether the object is an instance of the specified class or one of its subclasses or implements the specified interface.

Example:

public class OperatorExample{  
  public static void main(String[] args) {
    	Object obj = "Hello";
    	boolean isString = obj instanceof String;
    	System.out.println("Is it a String? " + isString); // Output: Is it a String? true
   
    }
}

Output:

Is it a String? true

3. Assignment Operators:

Definition: Assignment operators do an addition or subtraction along with the assignment. In a single step, they are used to do an action and store the result in a variable.

Explanation: Instead of writing separate statements for calculation and assignment, assignment operators make the code more concise.

Examples: compound assignment operators: add:+=,subtract: -=, multiply: *=, divide: /=, modulus: %= .

Example:

public class OperatorExample{
  public static void main(String[] args) {
    	int m = 20;
    	m += 5; // Equivalent to m = m + 5, so m becomes 25
    	System.out.println("m: " + m); 
  }
}

Output:

m: 25

4. Conditional-AND Operator (&&) and Conditional-OR Operator (||):

Used for short-circuiting logical evaluations.

The conditional-AND (&&) operator only evaluates the second operand if the first operand is true.

The conditional-OR (||) operator only evaluates the second operand if the first operand is false.

Example:

public class OperatorExample{  
   public static void main(String[] args) {
    	int s = 10;
    	int t = 5;
    	boolean result = (s > 0) && (t < 10);
    	System.out.println("Result: " + result); // Output: Result: true

   }
}

Output:

Result: true

Example:

public class OperatorExample{
  public static void main(String[] args) {
   	int s = 10;
   	int t = 5;
   	boolean result = (s < 0) || (t > 10);
   	System.out.println("Result: " + result); // Output: Result: false

   }
}

Output:

Result: false

Visited 1 times, 1 visit(s) today

Comments are closed.

Close