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 Java in this page.

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

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription