Java: Miscellaneous Operators

miscellaneous operators in java

Operators

Java provides quite a variety of operators which come in handy to the programmer for the manipulation of variables.we will see Miscellaneous Operators in this page

The operators in java are as follows:

  • Arithmetic Operators
  • Relational Operators
  • Bitwise Operators
  • Logical Operators
  • Assignment Operators
Apart from these operators there is another category of operators namely:
  • Ternary operators.
  • Unary Operators

Miscellaneous Operators

Ternary Operators

A ternary operator can be seen as an alternative to the if-else statement. It works the same as an if-else statement. It evaluates a test condition and executes the block of code accordingly.

Here, the condition is evaluated and

  • If the condition is true, expression1 is executed.
  • If the condition is false, expression2 is executed.

Syntax

condition ? expression1 : expression2;

Example

public class Main 
{
  public static void main(String[] args) 
  {

int marks = 56;
String result = (marks > 50) ? "passed" : "failed";

System.out.println("Hey Prepster You " + result + " the exam.");
}
}

Output

Hey, Prepster you passed the exam.

Unary Operators

Unary operators are the category of operators that require just a single operand to perform operations on. These are as follows:
Operator Functionality Syntax
+ Unary plus operator +a
Unary minus operator -a
++ Increment a++
/ Decrement a–

Example

public class Main 
{
public static void main(String[] args)
{

int num = +50;
System.out.println(num);

num --;
System.out.println(num);

num ++;
System.out.println(num);

num = - num;
System.out.println(num);

}
}

Output

50

49

50

-50