Previous | Home | Next |
Java provides a rich set of operators. Operators can be used to perform an action on one or two operands. An operator that can operate on one operand is as called unary oprator and an operator which can operate on two operands is called binary operator.
Java operators are divided in different categories.Assignment, increment and decrement, equality, arithmetic ,relational , logical, bit-wise, compound assignment, and conditional operator .
- Assignment operators( =,+=,-=,*=,/=,%=)
- Arithmetic operators are( +, - ,/, *, %)
- Increment and Decrement Operators(++,--)
- Relational operators(<, > ,<= ,>=, instanceof)
- Equality operator(==,!=)
- Logical operators(&&, | | , !.)
- && is logical and:
- || is logical OR:
- ! which means not:
- Conditional operators (?)
- Bitwis operator(&,|,^,~,<<,>>>>)
Assignment operator is mainly used to assign value to the variables. This operator can also be used on objects to assign object references. These operators are same as in c/c++. It is represented by "=" symbol in Java which is used to assign a value to a variable lying to the left side of the assignment operator.
This operator can also be used to assign the references to the objects.
Syntax :
<variable> = <expression>
int i 4; int j=8; //assignment operator = is used to assign the value to the variables i, and j ,the values assigning are 4and 8 respectively.
Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra.
int i=9; int j=0; int x; x= i+j; // operator is used to add the two integer values.
Incrementing and Decrementing operators in Java Programming are (++ )and (- - )operators.In computer programming it is quite common to want to increase or decrease the value of an integer type by 1. Because of this Java provides the increment and decrement operators that add 1 to a variable and subtract 1 from a variable, respectively. The increment operator is denoted by two plus signs (++), and the decrement operator is denoted by two minus signs (--).
variable++; ++variable; variable--; --variable; int a=5,b; b=++a*++a; System.out.println(b); //42 a=5; System.out.print(++a*++a); //42
Relational Operators are important to any programming language to make comparisons between values. These operator are same as c or c++ but <,<=,>,>= can not be applied on boolean types and reference types.
int x=10; int y=21; if(x>y) // relation check between x and y. { system .out.println ( "the greater no is:" + x); } else { system .out. println ("the greater no is :" + y); }
These operator are same as c or c++.
System.out.print(5==4); //false System.out.print(5!=4); //true
NOT (!) - Performs the negation of a statement (True changed to false, false changes to true).
AND (&&) - Performs the conjunction of two statements.
OR (||) - Performs the disjunction of two statements.
combines two boolean values and returns a boolean which is true if and only if both of its operands are true.
|| combines two boolean variables or expressions and returns a result that is true if either or both of its operands are true.
It reverses the value of a boolean expression. Thus if b is true !b is false. If b is false !b is true.
These operator are same as c or c++.
int a=5; if(a>5&&a++>5) { System.out.print("Hello"); } else { System.out.print("Bye"); } System.out.print(a);
output :
Bye
These operator are same as c or c++.
int x = 8; int y = 9; if (x== 1) && (y==2) { system .out.println ("the value of x is 8 AND value of y is 9"); }
The Java programming language also provides operators that perform bitwise and bit shift operations on integral types. The unary bitwise complement operator "~"convert a bit pattern. It can be applied to any of the integral types, making every "0" a "1" and every "1" a "0".
Example:
a value whose bit pattern is "00000000" would change its pattern to "11111111".
The signed left shift operator "<<" shifts a bit pattern to the left,and the signed right shift operator ">>" shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.
The bitwise & operator performs a bitwise AND operation.
The bitwise ^ operator performs a bitwise exclusive OR operation.
The bitwise | operator performs a bitwise inclusive OR operation
~ Unary bitwise complement
<< Signed left shift
>> Signed right shift
>>> Unsigned right shift
& Bitwise AND
^ Bitwise exclusive OR
| Bitwise inclusive OR
Previous | Home | Next |