Operators in Java
Java provides many type of operator , which can be used according to the need . they are classified based on the functionality.
"In Java Operator Are a symbol that are used for performing specific operations in java , operators make task like Addition, multiplication etc "
Type of operators
There are multiple operator in Java language
- Arithmetic operator
- Assignment operator
- Relational operator
- Logical Operator
- Ternary Operator
- Bitwise operator
- Shift operator
- Instance operator

|
Java Operators
1. Arithmetic operator They are used to perform simple arithmetic operations on primitive data types.
- * : Multiplication
- / : Division
- % : Modulo
- + : Addition
- – : Subtraction
For example :-
int a = 10 ;
int b = 3 ;
System.out.println( "a + b = " + (a + b));
System.out.println( "a - b = " + (a - b));
System.out.println( "a * b = " + (a * b));
System.out.println( "a / b = " + (a / b));
System.out.println( "a % b = " + (a % b));
Outputa + b = 13
a - b = 7
a * b = 30
a / b = 3
a % b = 1 2.Unary Operator Unary operators need only one operand. They are used to increment, decrement, or negate a value. (++) Increment operator used for incrementing the value by 1. There are two varieties of increment operators.
- Post-Increment: Value is first used for computing the result and then incremented.
- Pre-Increment: Value is incremented first, and then the result is computed.
(– – ) Decrement operator
used for decrementing the value by 1. There are two varieties of decrement operators. - Post-decrement: Value is first used for computing the result and then decremented.
- Pre-Decrement: The value is decremented first, and then the result is computed.
(!)Logical not operator
used for inverting a boolean value. For example :- // Interger declared int a = 10 ;
int b = 10 ;
System.out.println( "Postincrement : " + (a++));
System.out.println( "Preincrement : " + (++a));
System.out.println( "Postdecrement : " + (b--));
System.out.println( "Predecrement : " + (--b));
OutputPostincrement : 10
Preincrement : 12
Postdecrement : 10
Predecrement : 8 3. Assignment Operator ‘=’ Assignment operator is used to assign a value to any variable. It has right-to-left associativity,
variable = value; In many cases, the assignment operator can be combined with other operators to build a shorter version of the statement called a Compound Statement For example, instead of a = a+5, we can write a += 5. 4. Relational OperatorsThese operators are used to check for relations like equality, greater than, and less than. They return boolean results after the comparison
variable relation_operator value Some of the relational operators are- - ==, Equal to returns true if the left-hand side is equal to the right-hand side.
- !=, Not Equal to returns true if the left-hand side is not equal to the right-hand side.
- <, less than: returns true if the left-hand side is less than the right-hand side.
- <=, less than or equal to returns true if the left-hand side is less than or equal to the right-hand side.
- >, Greater than: returns true if the left-hand side is greater than the right-hand side.
- >=, Greater than or equal to returns true if the left-hand side is greater than or equal to the right-hand side.
For example
int a = 10 ;
int b = 3 ;
int c = 5 ;
System.out.println( "a > b: " + (a > b));
System.out.println( "a < b: " + (a < b));
System.out.println( "a >= b: " + (a >= b));
System.out.println( "a <= b: " + (a <= b));
System.out.println( "a == c: " + (a == c));
System.out.println( "a != c: " + (a != c));
Outputa > b: true
a < b: false
a >= b: true
a <= b: false
a == c: false
a != c: true 5. Logical OperatorsThese operators are used to perform “logical AND” and “logical OR” operations
Conditional operators are: - &&, Logical AND: returns true when both conditions are true.
- ||, Logical OR: returns true if at least one condition is true.
- !, Logical NOT: returns true when a condition is false and vice-versa
6. Ternary operatorThe ternary operator is a shorthand version of the if-else statement. It has three operands and hence the name Ternary.
condition ? if true : if false 7. Bitwise OperatorsThese operators are used to perform the manipulation of individual bits of a number. They can be used with any of the integer types &, Bitwise AND operator: returns bit by bit AND of input values. |, Bitwise OR operator: returns bit by bit OR of input values. ^, Bitwise XOR operator: returns bit-by-bit XOR of input values. ~, Bitwise Complement Operator: This is a unary operator which returns the one’s complement representation of the input value, i.e., with all bits inverted.
8. Shift OperatorsThese operators are used to shift the bits of a number left or right, thereby multiplying or dividing the number by two
<<, Left shift operator: shifts the bits of the number to the left and fills 0 on voids left as a result. Similar effect as multiplying the number with some power of two.>>, Signed Right shift operator: shifts the bits of the number to the right and fills 0 on voids left as a result. The leftmost bit depends on the sign of the initial number. Similar effect to dividing the number with some power of two.>>>, Unsigned Right shift operator: shifts the bits of the number to the right and fills 0 on voids left as a result.instance of operatorThe instance of the operator is used for type checking. It can be used to test if an object is an instance of a class
|
Comments
Post a Comment