You are on page 1of 6

JAVA - BASIC OPERATORS

http://www.tutorialspoint.com/java/java_basic_operators.htm Copyright © tutorials point.com

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

The Arit hmet ic Operat ors:


Arit hmet ic operat ors are used in mat hemat ical expressions in t he same way t hat t hey are used in
algebra. The following t able list s t he arit hmet ic operat ors:

Assume int eger variable A holds 10 and variable B holds 20, t hen:

Show Examples

Operato r Descriptio n Example

+ Addit ion - Adds values on eit her side of t he operat or A + B will give 30

- Subt ract ion - Subt ract s right hand operand from left hand A - B will give -10
operand

* Mult iplicat ion - Mult iplies values on eit her side of t he operat or A * B will give 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 B % A will give 0
ret urns remainder

++ Increment - Increases t he value of operand by 1 B++ gives 21

-- Decrement - Decreases t he value of operand by 1 B-- gives 19

The Relat ional Operat ors:


There are following relat ional operat ors support ed by Java language

Assume variable A holds 10 and variable B holds 20, t hen:

Show Examples

Operato r Descriptio n Example

== Checks if t he values of t wo operands are equal or not , if yes (A == B) is not


t hen condit ion becomes t rue. t rue.

!= Checks if t he values of t wo operands are equal or not , if values (A != B) is t rue.


are not equal t hen condit ion becomes t rue.
> Checks if t he value of left operand is great er t han t he value of (A > B) is not
right operand, if yes t hen condit ion becomes t rue. t rue.

< Checks if t he value of left operand is less t han t he value of right (A < B) is t rue.
operand, if yes t hen condit ion becomes t rue.

>= Checks if t he value of left operand is great er t han or equal t o (A >= B) is not
t he value of right operand, if yes t hen condit ion becomes t rue. t rue.

<= Checks if t he value of left operand is less t han or equal t o t he (A <= B) is t rue.
value of right operand, if yes t hen condit ion becomes t rue.

The Bit wise Operat ors:


Java defines several bit wise operat ors, which can be applied t o t he int eger t ypes, long, int , short ,
char, and byt e.

Bit wise operat or works on bit s and performs bit -by-bit operat ion. Assume if a = 60; and b = 13; now
in binary format t hey will be as follows:

a = 0011 1100

b = 0000 1101

-----------------

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a = 1100 0011

The following t able list s t he bit wise operat ors:

Assume int eger variable A holds 60 and variable B holds 13 t hen:

Show Examples

Operato r Descriptio n Example

& Binary AND Operat or copies a bit t o t he result if it exist s in bot h (A & B) will give
operands. 12 which is 0000
1100

| Binary OR Operat or copies a bit if it exist s in eit her operand. (A | B) will give 61
which is 0011
1101

^ Binary XOR Operat or copies t he bit if it is set in one operand but (A ^ B) will give
not bot h. 49 which is 0011
0001

~ Binary Ones Complement Operat or is unary and has t he effect of (~A ) will give -61
'flipping' bit s. which is 1100
0011 in 2's
complement
form due t o a
signed binary
number.

<< Binary Left Shift Operat or. The left operands value is moved left A << 2 will give
by t he number of bit s specified by t he right operan 240 which is 1111
0000
>> Binary Right Shift Operat or. The left operands value is moved A >> 2 will give
right by t he number of bit s specified by t he right operand. 15 which is 1111

>>> Shift right zero fill operat or. The left operands value is moved A >>>2 will give
right by t he number of bit s specified by t he right operand and 15 which is 0000
shift ed values are filled up wit h zeros. 1111

The Logical Operat ors:


The following t able list s t he logical operat ors:

Assume Boolean variables A holds t rue and variable B holds false, t hen:

Show Examples

Operato r Descriptio n Example

&& Called Logical AND operat or. If bot h t he operands are non-zero, (A && B) is false.
t hen t he condit ion becomes t rue.

|| Called Logical OR Operat or. If any of t he t wo operands are non- (A || B) is t rue.


zero, t hen t he condit ion becomes t rue.

! Called Logical NOT Operat or. Use t o reverses t he logical st at e of !(A && B) is t rue.
it s operand. If a condit ion is t rue t hen Logical NOT operat or will
make false.

The Assignment Operat ors:


There are following assignment operat ors support ed by Java language:

Show Examples

Operato r Descriptio n Example

= Simple assignment operat or, Assigns values from right side C = A + B will
operands t o left side operand assign value of A
+ B int o C

+= Add AND assignment operat or, It adds right operand t o t he left C += A is


operand and assign t he result t o left operand equivalent t o C =
C+A

-= Subt ract AND assignment operat or, It subt ract s right operand C -= A is
from t he left operand and assign t he result t o left operand equivalent t o C =
C-A

*= Mult iply AND assignment operat or, It mult iplies right operand C *= A is
wit h t he left operand and assign t he result t o left operand equivalent t o C =
C*A

/= Divide AND assignment operat or, It divides left operand wit h t he C /= A is


right operand and assign t he result t o left operand equivalent t o C =
C/A

%= Modulus AND assignment operat or, It t akes modulus using t wo C %= A is


operands and assign t he result t o left operand equivalent t o C =
C%A

<<= Left shift AND assignment operat or C <<= 2 is same


as C = C << 2
>>= Right shift AND assignment operat or C >>= 2 is same
as C = C >> 2

&= Bit wise AND assignment operat or C &= 2 is same


as C = C & 2

^= bit wise exclusive OR and assignment operat or C ^= 2 is same


as C = C ^ 2

|= bit wise inclusive OR and assignment operat or C |= 2 is same as


C=C|2

Misc Operat ors


There are few ot her operat ors support ed by Java Language.

Condit ional Operat or ( ? : ):


Condit ional operat or is also known as t he t ernary operat or. This operat or consist s of t hree operands
and is used t o evaluat e Boolean expressions. The goal of t he operat or is t o decide which value
should be assigned t o t he variable. The operat or is writ t en as:

variable x = (expression) ? value if true : value if false

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 );

b = (a == 10) ? 20: 30;


System.out.println( "Value of b is : " + b );
}
}

This would produce t he following result :

Value of b is : 30
Value of b is : 20

inst anceof Operat or:


This operat or is used only for object reference variables. The operat or checks whet her t he object is
of a part icular t ype(class t ype or int erface t ype). inst anceof operat or is wriit en as:

( Object reference variable ) instanceof (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 would produce t he following result :

true

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 );
}
}

This would produce t he following result :

true

Precedence of Java Operat ors:


Operat or precedence det ermines t he grouping of t erms in an expression. This affect s how an
expression is evaluat ed. Cert ain operat ors have higher precedence t han ot hers; for example, t he
mult iplicat ion operat or has higher precedence t han t he addit ion operat or:

For example, x = 7 + 3 * 2; here x is assigned 13, not 20 because operat or * has higher precedence
t han +, so it first get s mult iplied wit h 3*2 and t hen adds int o 7.

Here, operat ors wit h t he highest precedence appear at t he t op of t he t able, t hose wit h t he lowest
appear at t he bot t om. Wit hin an expression, higher precedence operat ors will be evaluat ed first .

Catego ry Operato r Asso ciativity

Post fix () [] . (dot operat or) Left t oright

Unary ++ - - ! ~ Right t o left

Mult iplicat ive */% 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

Bit wise AND & Left t o right

Bit wise XOR ^ 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.

You might also like