Documente Academic
Documente Profesional
Documente Cultură
http://www.tutorialspoint.com/java/java_basic_operators.htm
Java provides a rich set of operat ors t o manipulat e variables. We can divide all t he Java operat ors int o
t he following groups:
Arit hmet ic Operat ors
Relat ional Operat ors
Bit wise Operat ors
Logical Operat ors
Assignment Operat ors
Misc Operat ors
Descriptio n
Example
A + B will give 30
Subt ract ion - Subt ract s right hand operand from left hand
operand
Mult iplicat ion - Mult iplies values on eit her side of t he operat or
B / A will give 2
B % A will give 0
++
B++ gives 21
--
B-- gives 19
Descriptio n
Example
==
(A == B) is not
t rue.
!=
(A != B) is t rue.
>
(A > B) is not
t rue.
<
(A < B) is t rue.
>=
(A >= B) is not
t rue.
<=
(A <= B) is t rue.
Descriptio n
Example
&
(A | B) will give 61
which is 0011
1101
(A ^ B) will give
49 which is 0011
0001
<<
Binary Left Shift Operat or. The left operands value is moved left
by t he number of bit s specified by t he right operan
>>
Binary Right Shift Operat or. The left operands value is moved
right by t he number of bit s specified by t he right operand.
>>>
Shift right zero fill operat or. The left operands value is moved
right by t he number of bit s specified by t he right operand and
shift ed values are filled up wit h zeros.
Descriptio n
Example
&&
(A && B) is false.
||
Called Logical OR Operat or. If any of t he t wo operands are nonzero, t hen t he condit ion becomes t rue.
(A || B) is t rue.
Descriptio n
Example
C = A + B will
assign value of A
+ B int o C
+=
C += A is
equivalent t o C =
C+A
-=
Subt ract AND assignment operat or, It subt ract s right operand
from t he left operand and assign t he result t o left operand
C -= A is
equivalent t o C =
C-A
*=
Mult iply AND assignment operat or, It mult iplies right operand
wit h t he left operand and assign t he result t o left operand
C *= A is
equivalent t o C =
C*A
/=
C /= A is
equivalent t o C =
C/A
%=
C %= A is
equivalent t o C =
C%A
<<=
C <<= 2 is same
as C = C << 2
>>=
C >>= 2 is same
as C = C >> 2
&=
C &= 2 is same
as C = C & 2
^=
C ^= 2 is same
as C = C ^ 2
|=
C |= 2 is same as
C=C|2
Following is t he example:
public class Test {
public static void main(String args[]){
int a , b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " +
b );
(class/interface type)
If t he object referred by t he variable on t he left side of t he operat or passes t he IS-A check for t he
class/int erface t ype on t he right side, t hen t he result will be t rue. Following is t he example:
public class Test {
public static void main(String args[]){
String name = "James";
// following will return true since name is type of String
boolean result = name instanceof String;
System.out.println( result );
}
}
This operat or will st ill ret urn t rue if t he object being compared is t he assignment compat ible wit h t he
t ype on t he right . Following is one more example:
class Vehicle {}
public class Car extends Vehicle {
public static void main(String args[]){
Vehicle a = new Car();
boolean result = a instanceof Car;
System.out.println( result );
}
}
Operato r
Asso ciativity
Post fix
Left t oright
Unary
++ - - ! ~
Right t o left
*/%
Left t o right
Addit ive
+-
Left t o right
Shift
Left t o right
Relat ional
Left t o right
Equalit y
== !=
Left t o right
&
Left t o right
Left t o right
Bit wise OR
Left t o right
Logical AND
&&
Left t o right
Logical OR
||
Left t o right
Condit ional
?:
Right t o left
Assignment
Right t o left
Comma
Left t o right
What is Next ?
Next chapt er would explain about loop cont rol in Java programming. The chapt er will describe various
t ypes of loops and how t hese loops can be used in Java program development and for what
purposes t hey are being used.