Java: Miscellaneous Operators
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.
The operators in java are as follows:
Arithmetic Operators
Relational Operators
Bitwise Operators
Logical Operators
Assignment Operators
Miscellaneous Operators
Relational Operators
Bitwise Operators
Logical Operators
Assignment Operators
Miscellaneous Operators
Apart from these operators there is another category of operators namely:
Ternary operators.
Unary 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
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
Login/Signup to comment