Java Operators
- Categories Operators, Java Operators, Java
Here we are going to learn above Java Operators. You can watch our video on Java Operators – Click Here
Q. 1 What are operators?
Ans. Operators are special symbols that perform specific operations on Operands
e.g. +, – , <,
Q. 2 What are three classifications of operators?
Ans. Java operators can be classified as unary, binary, or ternary—meaning taking one, two, or three arguments, respectively.
- Unary: Increment ++, negation –
- Binary: a + b, num = c
- Ternary: Conditional Operator ?
Q.3 What is assignment operator?
Ans. Assignment operators is used to assign a value to a variable
Syntax: < variable > = < expression >
e.g.
double time = 2.10d;
char c = ‘c’;
int counter = 1;
String name = “Nisha”;
boolean rs = true;
counter = 5; // previous value is overwritten
Q. 4 What are arithmetic operators?
Ans. Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. There are 8 types of Arithmetic operators
- Plus (+), Minus (-), Multiply (*), Divide (/)
- Modulus/ Reminder (%)
- Increment (++), Decrements (–)
Assuming A is 10 and B is 20
Operator | Description | Example |
---|---|---|
+ | Addition – Adds values on either side of the operator | A + B will give 30 |
– | Subtraction – Subtracts right hand operand from left hand operand | A – B will give -10 |
* | Multiplication – Multiplies values on either side of the operator | A * B = 200 |
/ | Division – Divides left hand operand by right hand operand | B / A will give 2 |
% | Modulus – Divides left hand operand by right hand operand and returns remainder | B % A will give 0 |
++ | Increment – Increase the value of operand by 1 | B++ gives 21 |
— | Decrement – Decrease the value of operand by 1 | B– gives 19 |
Q. 5 What are relational operators?
Ans. Relational operators are used to compare two operands. It returns only Boolean value either true or false.
E.g <, >,<=, >=, ==, !=
Operator | Description | Example |
---|---|---|
== | Checks if the value of two operands are equal or not, if yes then condition becomes true. | (A == B) is not true. |
!= | Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. | (A != B) is true. |
> | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (A > B) is not true. |
< | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (A < B) is true. |
>= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (A >= B) is not true. |
<= | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (A <= B) is true. |
Q.6 What are logical operators?
Ans. Logical Operators are used to combine one or more relational expressions. It returns true or false value. It operates only on boolean values. E.g a = (b < c) && (d<e)
Operator | Description | Example |
---|---|---|
&& | Logical AND operator. If both the operands are non zero then then condition becomes true. | (A && B) is false. |
|| | Logical OR Operator. If any of the two operands are non zero then then condition becomes true. | (A || B) is true. |
! | Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(A && B) is true. |
Q. 7 What is ternary operator in Java?
Ans. Conditional or ternary is the only three argument operator in Java. This operator consists of three operands and is used to evaluate boolean expressions.
Syntax: variable x = (expression) ? value if true : value if false
E.g. b = (a == 1) ? 20: 30;
Q. 8 What is operator precedence?
Ans. The order in which operators are applied is known as precedence. Operators with a higher precedence are applied before operators with a lower precedence.